<file_sep>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_ML_MODEL_IMPL_MAC_H_
#define SERVICES_ML_MODEL_IMPL_MAC_H_

#include "common.h"

namespace ml {

class CompilationImplMac;

class ModelImplMac {
 public:
  ModelImplMac();
  ~ModelImplMac();

  int32_t AddOperand(int32_t type, const std::vector<uint32_t>& dimensions, float scale, int32_t zeroPoint);
  int32_t SetOperandValue(uint32_t index, const std::vector<float>& data);
  int32_t AddOperation(int32_t type, const std::vector<uint32_t>& inputs, const std::vector<uint32_t>& outputs);
  int32_t IdentifyInputsAndOutputs(const std::vector<uint32_t>& inputs, const std::vector<uint32_t>& outputs);
  
//  void Finish(mojom::ModelInfoPtr model_info, FinishCallback callback);
  void CreateCompilation();

 private:
  friend class CompilationImplMac;
  std::vector<Operand> operands_;
  std::vector<Operation> operations_;
  std::map<uint32_t, ValueInfo> values_;
  std::vector<uint32_t> inputs_;
  std::vector<uint32_t> outputs_;
};

}  // namespace ml

#endif  // SERVICES_ML_MODEL_IMPL_MAC_H_
<file_sep>//
//  main.cpp
//  MPSNativeSample
//
//  Created by mac-webgl-stable on 1/31/19.
//  Copyright © 2019 mac-webgl-stable. All rights reserved.
//

#include <iostream>
#include "test_cases.h"
#include "depthwise_conv_test.h"
#include "resize_bilinear_test.h"
#include "average_pool_test.h"

int main(int argc, const char * argv[]) {
//  ml::ConvFloat();
  
//  ml::Depthwise28_28Conv5_5();
//
//  ml::Depthwise28_28_528Conv5_5_528();
  
//  ml::ResizeBilinear65_65To513_513();
//
//  ml::ResizeBilinear65_65_21To513_513_21();
  
  ml::AveragePool();
  
  return 0;
}
<file_sep>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_ML_DEPTHWISE_CONV_TEST_H_
#define SERVICES_ML_DEPTHWISE_CONV_TEST_H_

namespace ml {

void DepthwiseConv2dFloatLarge();
  
void Depthwise28_28Conv5_5();

void Depthwise28_28_528Conv5_5_528();
}  // namespace ml

#endif  // SERVICES_ML_DEPTHWISE_CONV_TEST_H_
<file_sep>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "common.h"

#include "constants.h"

namespace ml {

uint32_t product(const std::vector<uint32_t>& dims) {
  uint32_t prod = 1;
  for (size_t i = 0; i < dims.size(); ++i) prod *= dims[i];
  return prod;
}

uint32_t GetRequiredSize(int32_t type, const std::vector<uint32_t>& dimensions) {
  if (type == FLOAT32) {
    return sizeof(float);
  } else if (type == INT32) {
    return sizeof(int32_t);
  } else if (type == UINT32) {
    return sizeof(uint32_t);
  } else if (type == TENSOR_FLOAT32) {
    return product(dimensions) * sizeof(float);
  } else if (type == TENSOR_INT32) {
    return product(dimensions) * sizeof(int32_t);
  } else if (type == TENSOR_QUANT8_ASYMM) {
    return product(dimensions) * sizeof(int8_t);
  }
  return 0;
}

Operand::Operand() = default;
Operand::~Operand() = default;
Operand::Operand(const Operand&) = default;
uint32_t Operand::requiredSize() const {
  return GetRequiredSize(type, dimensions);
}

Operation::Operation() = default;
Operation::~Operation() = default;
Operation::Operation(const Operation&) = default;
//
//OperandInfo::OperandInfo(uint32_t length, void* mapping) :
//      length(length), mapping(std::move(mapping)) {}
//
//OperandInfo::~OperandInfo() {}

ValueInfo::ValueInfo() = default;
ValueInfo::~ValueInfo() = default;
ValueInfo::ValueInfo(const ValueInfo&) = default;

int32_t getScalarInt32(const ValueInfo& info, int8_t* memory) {
  return info.data[0];
}

// There are no viable overloaded operator[] for type
// 'const std::map<uint32_t, ValueInfo>', so uses 'find' instead of it.
int32_t getScalarInt32(const std::map<uint32_t, ValueInfo>& values,
                       uint32_t key,
                       int8_t* memory) {
  auto iter = values.find(key);
  if (iter == values.end()) {
    assert(0);
    return -1;
  }

  return getScalarInt32(iter->second, memory);
}

float getScalarFloat(const ValueInfo& info, int8_t* memory) {
  return info.data[0];
}

float getScalarFloat(const std::map<uint32_t, ValueInfo>& values,
                     uint32_t key,
                     int8_t* memory) {
  auto iter = values.find(key);
  if (iter == values.end()) {
    assert(0);
    return -1.0;
  }

  return getScalarFloat(iter->second, memory);
}

}
<file_sep>//
//  constants.h
//  MPSNativeSample
//
//  Created by mac-webgl-stable on 1/31/19.
//  Copyright © 2019 mac-webgl-stable. All rights reserved.
//

#ifndef constants_h
#define constants_h

// Operand types.
const int32_t FLOAT32 = 0;
const int32_t INT32 = 1;
const int32_t UINT32 = 2;
const int32_t TENSOR_FLOAT32 = 3;
const int32_t TENSOR_INT32 = 4;
const int32_t TENSOR_QUANT8_ASYMM = 5;

// Operation types.
const int32_t ADD = 0;
const int32_t AVERAGE_POOL_2D = 1;
const int32_t CONCATENATION = 2;
const int32_t CONV_2D = 3;
const int32_t DEPTHWISE_CONV_2D = 4;
const int32_t DEPTH_TO_SPACE = 5;
const int32_t DEQUANTIZE = 6;
const int32_t EMBEDDING_LOOKUP = 7;
const int32_t FLOOR = 8;
const int32_t FULLY_CONNECTED = 9;
const int32_t HASHTABLE_LOOKUP = 10;
const int32_t L2_NORMALIZATION = 11;
const int32_t L2_POOL_2D = 12;
const int32_t LOCAL_RESPONSE_NORMALIZATION = 13;
const int32_t LOGISTIC = 14;
const int32_t LSH_PROJECTION = 15;
const int32_t LSTM = 16;
const int32_t MAX_POOL_2D = 17;
const int32_t MUL = 18;
const int32_t RELU = 19;
const int32_t RELU1 = 20;
const int32_t RELU6 = 21;
const int32_t RESHAPE = 22;
const int32_t RESIZE_BILINEAR = 23;
const int32_t RNN = 24;
const int32_t SOFTMAX = 25;
const int32_t SPACE_TO_DEPTH = 26;
const int32_t SVDF = 27;
const int32_t TANH = 28;
const int32_t ATROUS_CONV_2D = 10003;
const int32_t ATROUS_DEPTHWISE_CONV_2D = 10004;

// Fused activation function types.
const int32_t FUSED_NONE = 0;
const int32_t FUSED_RELU = 1;
const int32_t FUSED_RELU1 = 2;
const int32_t FUSED_RELU6 = 3;

// Implicit padding algorithms.
const int32_t PADDING_SAME = 1;
const int32_t PADDING_VALID = 2;

// Execution preferences.
const int32_t PREFER_LOW_POWER = 0;
const int32_t PREFER_FAST_SINGLE_ANSWER = 1;
const int32_t PREFER_SUSTAINED_SPEED = 2;

const int32_t NOT_ERROR = 0;
const int32_t OUT_OF_MEMORY = 1;
const int32_t INCOMPLETE = 2;
const int32_t UNEXPECTED_NULL = 3;
const int32_t BAD_DATA = 4;
const int32_t OP_FAILED = 5;
const int32_t UNMAPPABLE = 5;
const int32_t BAD_STATE = 6;

#endif /* constants_h */
