mir::Operation::Output *CaffeOpCreator::convertCaffeToMIR(mir::Operation::Output *arg)
{
// NCHW -> NHWC
- auto transpose = createOp<ops::TransposeOp>("", arg, std::vector<std::size_t>{0, 2, 3, 1});
+ auto transpose = createOp<ops::TransposeOp>(arg, std::vector<std::size_t>{0, 2, 3, 1});
return transpose->getOutput(0);
}
mir::Operation::Output *CaffeOpCreator::convertMIRToCaffe(mir::Operation::Output *arg)
{
// NHWC -> NCHW
- auto transpose = createOp<ops::TransposeOp>("", arg, std::vector<std::size_t>{0, 3, 1, 2});
+ auto transpose = createOp<ops::TransposeOp>(arg, std::vector<std::size_t>{0, 3, 1, 2});
return transpose->getOutput(0);
}
std::vector<mir::Operation::Output *> outputs(num_parts);
for (int32_t i = 0; i < num_parts; ++i)
{
- outputs[i] = createOp<ops::SliceOp>("", arg, starts, sizes)->getOutput(0);
+ outputs[i] = createOp<ops::SliceOp>(arg, starts, sizes)->getOutput(0);
starts.dim(axis) += part_size;
}
for (int32_t i = axis; i < input_shape.rank(); ++i)
inner_size *= input_shape.dim(i);
- auto flatten = createOp<ops::ReshapeOp>("", input, Shape{outer_size, inner_size})->getOutput(0);
- auto fc = createOp<ops::FullyConnectedOp>("", flatten, weights)->getOutput(0);
- return createOp<ops::ReshapeOp>("", fc, result_shape)->getOutput(0);
+ auto flatten = createOp<ops::ReshapeOp>(input, Shape{outer_size, inner_size})->getOutput(0);
+ auto fc = createOp<ops::FullyConnectedOp>(flatten, weights)->getOutput(0);
+ return createOp<ops::ReshapeOp>(fc, result_shape)->getOutput(0);
}
TensorVariant CaffeOpCreator::convertBlob(const BlobProto &blob)
const auto &blob_name = layer.top(i);
const auto &blob_shape = params.shape(num_shapes == 1 ? 0 : i);
const mir::Shape shape = convertBlobShape(blob_shape);
- auto variable = createOp<ops::InputOp>(blob_name, shape);
+ auto variable = createOp<ops::InputOp>(shape);
+ variable->setName(blob_name);
outputs.push_back(variable->getOutput(0));
}
// This is depthwise convolution
// TODO handle properly kernel with layer multiplier
auto transposed_tensor = transposeTensor<0, 1, 3, 2>(kernel_weights);
- auto kernel = createOp<ops::ConstantOp>("", transposed_tensor)->getOutput(0);
- result = createOp<ops::DepthwiseConv2DOp>(layer.name(), convertCaffeToMIR(inputs[0]), kernel,
- strides, padding, padding)
+ auto kernel = createOp<ops::ConstantOp>(transposed_tensor)->getOutput(0);
+ result = createOp<ops::DepthwiseConv2DOp>(convertCaffeToMIR(inputs[0]), kernel, strides,
+ padding, padding)
->getOutput(0);
}
else
kernel_weights = fixGroupedKernel(params.group(), kernel_weights);
}
kernel_weights = transposeTensor<3, 0, 1, 2>(kernel_weights);
- auto kernel = createOp<ops::ConstantOp>("", kernel_weights)->getOutput(0);
- result = createOp<ops::Conv2DOp>(layer.name(), convertCaffeToMIR(inputs[0]), kernel, strides,
- padding, padding)
- ->getOutput(0);
+ auto kernel = createOp<ops::ConstantOp>(kernel_weights)->getOutput(0);
+ result =
+ createOp<ops::Conv2DOp>(convertCaffeToMIR(inputs[0]), kernel, strides, padding, padding)
+ ->getOutput(0);
}
// Add the bias, if any.
if (params.bias_term())
{
- auto bias = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(1)))->getOutput(0);
- result = createOp<ops::AddOp>(layer.name() + ".bias", result, bias)->getOutput(0);
+ auto bias = createOp<ops::ConstantOp>(convertBlob(layer.blobs(1)))->getOutput(0);
+ result = createOp<ops::AddOp>(result, bias)->getOutput(0);
}
return {convertMIRToCaffe(result)};
// first we need to convert kernel of grouped convolution to appropriate ordinary kernel
kernel_weights = fixGroupedKernel(opts.group(), kernel_weights);
}
- auto kernel = createOp<ops::ConstantOp>("", kernel_weights)->getOutput(0);
- auto result = createOp<ops::DeConv2DOp>(layer.name(), convertCaffeToMIR(inputs[0]), kernel,
- strides, padding)
+ auto kernel = createOp<ops::ConstantOp>(kernel_weights)->getOutput(0);
+ auto result = createOp<ops::DeConv2DOp>(convertCaffeToMIR(inputs[0]), kernel, strides, padding)
->getOutput(0);
// bias_term is optional (so might not be present) and defaults to true
if (opts.bias_term())
{
- auto bias = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(1)))->getOutput(0);
- result = createOp<ops::AddOp>(layer.name() + ".bias", result, bias)->getOutput(0);
+ auto bias = createOp<ops::ConstantOp>(convertBlob(layer.blobs(1)))->getOutput(0);
+ result = createOp<ops::AddOp>(result, bias)->getOutput(0);
}
return {convertMIRToCaffe(result)};
if (!params.transpose())
weights_tensor = transposeTensor<1, 0>(weights_tensor);
- auto weights = createOp<ops::ConstantOp>("", weights_tensor)->getOutput(0);
+ auto weights = createOp<ops::ConstantOp>(weights_tensor)->getOutput(0);
auto result = createFullyConnected(inputs[0], weights, params.axis());
// Add the bias, if any.
if (params.bias_term())
{
- auto bias = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(1)))->getOutput(0);
- result = createOp<ops::AddOp>(layer.name() + ".bias", result, bias)->getOutput(0);
+ auto bias = createOp<ops::ConstantOp>(convertBlob(layer.blobs(1)))->getOutput(0);
+ result = createOp<ops::AddOp>(result, bias)->getOutput(0);
}
return {result};
const std::vector<mir::Operation::Output *> &inputs)
{
const auto ¶ms = layer.concat_param();
- auto concat = createOp<ops::ConcatOp>(layer.name(), inputs, params.axis());
+ auto concat = createOp<ops::ConcatOp>(inputs, params.axis());
return {concat->getOutput(0)};
}
assert(false);
}
- auto pooling =
- createOp<ops::PoolOp>(layer.name(), convertCaffeToMIR(inputs[0]), pool_type, window_shape,
- strides, padding_before, padding_after, border_type);
+ auto pooling = createOp<ops::PoolOp>(convertCaffeToMIR(inputs[0]), pool_type, window_shape,
+ strides, padding_before, padding_after, border_type);
return {convertMIRToCaffe(pooling->getOutput(0))};
}
if (params.axis() != 1)
throw std::runtime_error("Softmax: unsupported axis");
int32_t axis = 3;
- auto input = createOp<ops::TransposeOp>(layer.name() + ".trans1", inputs[0],
- std::vector<std::size_t>{0, 2, 3, 1});
- auto softmax = createOp<ops::SoftmaxOp>(layer.name(), input->getOutput(0), axis);
- auto result = createOp<ops::TransposeOp>(layer.name() + ".trans2", softmax->getOutput(0),
- std::vector<std::size_t>{0, 3, 1, 2});
+ auto input = createOp<ops::TransposeOp>(inputs[0], std::vector<std::size_t>{0, 2, 3, 1});
+ auto softmax = createOp<ops::SoftmaxOp>(input->getOutput(0), axis);
+ auto result =
+ createOp<ops::TransposeOp>(softmax->getOutput(0), std::vector<std::size_t>{0, 3, 1, 2});
return {result->getOutput(0)};
}
- auto softmax = createOp<ops::SoftmaxOp>(layer.name(), inputs[0], params.axis());
+ auto softmax = createOp<ops::SoftmaxOp>(inputs[0], params.axis());
return {softmax->getOutput(0)};
}
{
auto &opts = layer.reshape_param();
const mir::Shape new_shape = convertBlobShape(opts.shape());
- auto reshape = createOp<ops::ReshapeOp>(layer.name(), inputs[0], new_shape);
+ auto reshape = createOp<ops::ReshapeOp>(inputs[0], new_shape);
return {reshape->getOutput(0)};
}
if (layer.relu_param().has_negative_slope())
{
float alpha = layer.relu_param().negative_slope();
- relu = createOp<ops::LeakyReluOp>(layer.name(), inputs[0], alpha);
+ relu = createOp<ops::LeakyReluOp>(inputs[0], alpha);
}
else
{
- relu = createOp<ops::ReluOp>(layer.name(), inputs[0]);
+ relu = createOp<ops::ReluOp>(inputs[0]);
}
return {relu->getOutput(0)};
const std::vector<mir::Operation::Output *> &inputs)
{
const auto ¶ms = layer.scale_param();
- auto scale = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(0)))->getOutput(0);
- auto result =
- createOp<ops::MulOp>(layer.name(), convertCaffeToMIR(inputs[0]), scale)->getOutput(0);
+ auto scale = createOp<ops::ConstantOp>(convertBlob(layer.blobs(0)))->getOutput(0);
+ auto result = createOp<ops::MulOp>(convertCaffeToMIR(inputs[0]), scale)->getOutput(0);
// Add the bias, if any.
if (params.bias_term())
{
- auto bias = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(1)))->getOutput(0);
- result = createOp<ops::AddOp>(layer.name() + ".bias", result, bias)->getOutput(0);
+ auto bias = createOp<ops::ConstantOp>(convertBlob(layer.blobs(1)))->getOutput(0);
+ result = createOp<ops::AddOp>(result, bias)->getOutput(0);
}
return {convertMIRToCaffe(result)};
Tensor<float> mean_accessor(mean_tensor);
for (const auto &idx : ShapeRange(mean_accessor.getShape()))
mean_accessor.at(idx) *= -scale_factor;
- auto c1 = createOp<ops::ConstantOp>("", mean_tensor)->getOutput(0);
+ auto c1 = createOp<ops::ConstantOp>(mean_tensor)->getOutput(0);
// C2 = 1 / sqrt(var / scale_factor + epsilon)
Tensor<float> var_accessor(var_tensor);
for (const auto &idx : ShapeRange(var_accessor.getShape()))
var_accessor.at(idx) = 1.0f / std::sqrt(var_accessor.at(idx) * scale_factor + eps);
- auto c2 = createOp<ops::ConstantOp>("", var_tensor)->getOutput(0);
+ auto c2 = createOp<ops::ConstantOp>(var_tensor)->getOutput(0);
// Y = (X + C1) * C2
input = convertCaffeToMIR(input);
- auto result = createOp<ops::AddOp>("", input, c1)->getOutput(0);
- result = createOp<ops::MulOp>("", result, c2)->getOutput(0);
+ auto result = createOp<ops::AddOp>(input, c1)->getOutput(0);
+ result = createOp<ops::MulOp>(result, c2)->getOutput(0);
return {convertMIRToCaffe(result)};
}
const std::vector<mir::Operation::Output *> &inputs)
{
auto &opts = layer.elu_param();
- auto elu = createOp<ops::EluOp>(layer.name(), inputs[0], opts.alpha());
+ auto elu = createOp<ops::EluOp>(inputs[0], opts.alpha());
return {elu->getOutput(0)};
}
const std::vector<mir::Operation::Output *> &inputs)
{
const auto ¶ms = layer.embed_param();
- auto data = createOp<ops::ConstantOp>(layer.name() + ".weights", convertBlob(layer.blobs(0)));
- auto result =
- createOp<ops::GatherOp>(layer.name(), data->getOutput(0), inputs[0], 0)->getOutput(0);
+ auto data = createOp<ops::ConstantOp>(convertBlob(layer.blobs(0)));
+ auto result = createOp<ops::GatherOp>(data->getOutput(0), inputs[0], 0)->getOutput(0);
// Add the bias, if any.
if (params.bias_term())
{
- auto bias = createOp<ops::ConstantOp>("", convertBlob(layer.blobs(1)))->getOutput(0);
- result = createOp<ops::AddOp>(layer.name() + ".bias", result, bias)->getOutput(0);
+ auto bias = createOp<ops::ConstantOp>(convertBlob(layer.blobs(1)))->getOutput(0);
+ result = createOp<ops::AddOp>(result, bias)->getOutput(0);
}
return {result};
CaffeOpCreator::convertSigmoid(const caffe::LayerParameter &layer,
const std::vector<mir::Operation::Output *> &inputs)
{
- auto result = createOp<ops::SigmoidOp>(layer.name(), inputs[0]);
+ auto result = createOp<ops::SigmoidOp>(inputs[0]);
return {result->getOutput(0)};
}
CaffeOpCreator::convertTanH(const caffe::LayerParameter &layer,
const std::vector<mir::Operation::Output *> &inputs)
{
- auto tanh = createOp<ops::TanhOp>(layer.name(), inputs[0]);
+ auto tanh = createOp<ops::TanhOp>(inputs[0]);
return {tanh->getOutput(0)};
}
{
case EltwiseParameter_EltwiseOp_PROD:
{
- result = createOp<ops::MulOp>("", inputs[0], inputs[1])->getOutput(0);
+ result = createOp<ops::MulOp>(inputs[0], inputs[1])->getOutput(0);
for (int i = 2; i < layer.bottom_size(); ++i)
{
- result = createOp<ops::MulOp>("", result, inputs[i])->getOutput(0);
+ result = createOp<ops::MulOp>(result, inputs[i])->getOutput(0);
}
break;
}
{
const float coeff_val = params.coeff(i);
TensorVariant coeff_tensor(mir::DataType::FLOAT32, Shape{}, &coeff_val);
- auto coeff_const = createOp<ops::ConstantOp>("", coeff_tensor)->getOutput(0);
- scaled_inputs[i] = createOp<ops::MulOp>("", coeff_const, inputs[i])->getOutput(0);
+ auto coeff_const = createOp<ops::ConstantOp>(coeff_tensor)->getOutput(0);
+ scaled_inputs[i] = createOp<ops::MulOp>(coeff_const, inputs[i])->getOutput(0);
}
}
}
- result = createOp<ops::AddOp>("", scaled_inputs[0], scaled_inputs[1])->getOutput(0);
+ result = createOp<ops::AddOp>(scaled_inputs[0], scaled_inputs[1])->getOutput(0);
for (int i = 2; i < layer.bottom_size(); ++i)
{
- result = createOp<ops::AddOp>("", result, scaled_inputs[i])->getOutput(0);
+ result = createOp<ops::AddOp>(result, scaled_inputs[i])->getOutput(0);
}
break;
}
case EltwiseParameter_EltwiseOp_MAX:
{
- result = createOp<ops::MaxOp>("", inputs[0], inputs[1])->getOutput(0);
+ result = createOp<ops::MaxOp>(inputs[0], inputs[1])->getOutput(0);
for (int i = 2; i < layer.bottom_size(); ++i)
{
- result = createOp<ops::MaxOp>("", result, inputs[i])->getOutput(0);
+ result = createOp<ops::MaxOp>(result, inputs[i])->getOutput(0);
}
break;
}
auto xw_tensor = transposeTensor<1, 0>(convertBlob(layer.blobs(0)));
auto xb_tensor = convertBlob(layer.blobs(1));
auto hw_tensor = transposeTensor<1, 0>(convertBlob(layer.blobs(2)));
- auto xw = createOp<ops::ConstantOp>("", xw_tensor)->getOutput(0);
- auto xb = createOp<ops::ConstantOp>("", xb_tensor)->getOutput(0);
- auto hw = createOp<ops::ConstantOp>("", hw_tensor)->getOutput(0);
+ auto xw = createOp<ops::ConstantOp>(xw_tensor)->getOutput(0);
+ auto xb = createOp<ops::ConstantOp>(xb_tensor)->getOutput(0);
+ auto hw = createOp<ops::ConstantOp>(hw_tensor)->getOutput(0);
// Add a dummy dimension so that element-wise operations perform properly.
- cont = createOp<ops::ReshapeOp>("", cont, Shape{seq_length, batch_size, 1})->getOutput(0);
+ cont = createOp<ops::ReshapeOp>(cont, Shape{seq_length, batch_size, 1})->getOutput(0);
// Initialize cell and hidden states with zeros.
auto zero_tensor = createZeroedTensor(Shape{1, batch_size, hidden_size});
- auto c_t = createOp<ops::ConstantOp>("", zero_tensor)->getOutput(0);
- auto h_t = createOp<ops::ConstantOp>("", zero_tensor)->getOutput(0);
+ auto c_t = createOp<ops::ConstantOp>(zero_tensor)->getOutput(0);
+ auto h_t = createOp<ops::ConstantOp>(zero_tensor)->getOutput(0);
auto x_xw = createFullyConnected(x, xw, 2);
- auto x_xw_b = createOp<ops::AddOp>("", x_xw, xb)->getOutput(0);
+ auto x_xw_b = createOp<ops::AddOp>(x_xw, xb)->getOutput(0);
// Split input and continuation tensors into seq_length slices.
std::vector<mir::Operation::Output *> x_xw_b_slices = createSplit(x_xw_b, seq_length, 0);
for (int32_t t = 0; t < seq_length; t++)
{
- auto c_cont_t = createOp<ops::MulOp>("", c_t, cont_slices[t])->getOutput(0);
- auto h_cont_t = createOp<ops::MulOp>("", h_t, cont_slices[t])->getOutput(0);
+ auto c_cont_t = createOp<ops::MulOp>(c_t, cont_slices[t])->getOutput(0);
+ auto h_cont_t = createOp<ops::MulOp>(h_t, cont_slices[t])->getOutput(0);
auto x_xw_b_t = x_xw_b_slices[t];
auto h_hw_t = createFullyConnected(h_cont_t, hw, 2);
- auto activation_inputs_concat = createOp<ops::AddOp>("", x_xw_b_t, h_hw_t)->getOutput(0);
+ auto activation_inputs_concat = createOp<ops::AddOp>(x_xw_b_t, h_hw_t)->getOutput(0);
auto activation_inputs = createSplit(activation_inputs_concat, 4, 2);
- auto i_t = createOp<ops::SigmoidOp>("", activation_inputs[0])->getOutput(0);
- auto f_t = createOp<ops::SigmoidOp>("", activation_inputs[1])->getOutput(0);
- auto o_t = createOp<ops::SigmoidOp>("", activation_inputs[2])->getOutput(0);
- auto g_t = createOp<ops::TanhOp>("", activation_inputs[3])->getOutput(0);
+ auto i_t = createOp<ops::SigmoidOp>(activation_inputs[0])->getOutput(0);
+ auto f_t = createOp<ops::SigmoidOp>(activation_inputs[1])->getOutput(0);
+ auto o_t = createOp<ops::SigmoidOp>(activation_inputs[2])->getOutput(0);
+ auto g_t = createOp<ops::TanhOp>(activation_inputs[3])->getOutput(0);
- c_t = createOp<ops::AddOp>("", createOp<ops::MulOp>("", c_cont_t, f_t)->getOutput(0),
- createOp<ops::MulOp>("", i_t, g_t)->getOutput(0))
+ c_t = createOp<ops::AddOp>(createOp<ops::MulOp>(c_cont_t, f_t)->getOutput(0),
+ createOp<ops::MulOp>(i_t, g_t)->getOutput(0))
->getOutput(0);
- h_t = createOp<ops::MulOp>("", createOp<ops::TanhOp>("", c_t)->getOutput(0), o_t)->getOutput(0);
+ h_t = createOp<ops::MulOp>(createOp<ops::TanhOp>(c_t)->getOutput(0), o_t)->getOutput(0);
h_slices[t] = h_t;
}
- return {createOp<ops::ConcatOp>("", h_slices, 0)->getOutput(0)};
+ return {createOp<ops::ConcatOp>(h_slices, 0)->getOutput(0)};
}
} // namespace mir_caffe