// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "orttraining/training_ops/cpu/math/scale.h" #include "core/framework/math.h" #include "core/providers/common.h" namespace onnxruntime { namespace contrib { #define REGISTER_SCALE_KERNEL_TYPED(T, ScaleT) \ ONNX_OPERATOR_TYPED_KERNEL_EX( \ Scale, \ kMSDomain, \ 1, \ T##_##ScaleT, \ kCpuExecutionProvider, \ KernelDefBuilder() \ .TypeConstraint("T", DataTypeImpl::GetTensorType()) \ .TypeConstraint("ScaleT", DataTypeImpl::GetTensorType()), \ Scale); template Scale::Scale(const OpKernelInfo& info) : OpKernel(info) { int64_t scale_down; info.GetAttrOrDefault("scale_down", &scale_down, static_cast(0)); scale_down_ = (scale_down != 0); } template Status Scale::Compute(OpKernelContext* context) const { const Tensor* scale_tensor = context->Input(1); ORT_ENFORCE(scale_tensor->Shape().Size() == 1, "Scale input should have a single value."); float scale_value = static_cast(*scale_tensor->Data()); ORT_ENFORCE(scale_value != 0.0f, "Scale value must not be 0."); if (scale_down_) { scale_value = 1.0f / scale_value; } const auto& input_tensor = *context->Input(0); auto& output_tensor = *context->Output(0, input_tensor.Shape()); EigenMap(output_tensor) = static_cast(scale_value) * EigenMap(input_tensor); return Status::OK(); } REGISTER_SCALE_KERNEL_TYPED(float, float) REGISTER_SCALE_KERNEL_TYPED(float, double) REGISTER_SCALE_KERNEL_TYPED(float, int64_t) REGISTER_SCALE_KERNEL_TYPED(float, int32_t) REGISTER_SCALE_KERNEL_TYPED(double, float) REGISTER_SCALE_KERNEL_TYPED(double, double) REGISTER_SCALE_KERNEL_TYPED(double, int64_t) REGISTER_SCALE_KERNEL_TYPED(double, int32_t) } // namespace contrib } // namespace onnxruntime