Compute Library
 22.08
CpuScale Class Reference

Basic function to compute Scale. More...

#include <CpuScale.h>

Collaboration diagram for CpuScale:
[legend]

Public Member Functions

void configure (ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
 Initialize the function's source, destination, interpolation type and border_mode. More...
 
void prepare (ITensorPack &tensors) override
 Prepare the function for executing. More...
 
void run (ITensorPack &tensors) override
 Run the kernels contained in the function. More...
 
- Public Member Functions inherited from INEOperator
 INEOperator (IRuntimeContext *ctx=nullptr)
 Constructor. More...
 
 INEOperator (const INEOperator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 INEOperator (INEOperator &&)=default
 Default move constructor. More...
 
INEOperatoroperator= (const INEOperator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
INEOperatoroperator= (INEOperator &&)=default
 Default move assignment operator. More...
 
 ~INEOperator ()
 Default destructor. More...
 
MemoryRequirements workspace () const override
 Return the memory requirements required by the workspace. More...
 
- Public Member Functions inherited from IOperator
virtual ~IOperator ()=default
 Destructor. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
 Static function to check if given info will lead to a valid configuration. More...
 

Detailed Description

Basic function to compute Scale.

Definition at line 40 of file CpuScale.h.

Member Function Documentation

◆ configure()

void configure ( ITensorInfo src,
ITensorInfo dst,
const ScaleKernelInfo info 
)

Initialize the function's source, destination, interpolation type and border_mode.

Parameters
[in,out]srcSource tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/U8/S16/F16/F32. (Written to only for border_mode != UNDEFINED)
[out]dstDestination tensor info. Data type supported: Same as src. All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
[in]infoScaleKernelInfo to be used for configuration

Definition at line 90 of file CpuScale.cpp.

References ScaleKernelInfo::align_corners, arm_compute::AREA, ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ARM_COMPUTE_LOG_PARAMS, arm_compute::BILINEAR, arm_compute::scale_utils::calculate_resize_ratio(), ScaleKernelInfo::data_layout, ITensorInfo::data_layout(), ITensorInfo::dimension(), arm_compute::test::validation::dst, arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, arm_compute::test::validation::idx_width, arm_compute::test::validation::info, ScaleKernelInfo::interpolation_policy, arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(), arm_compute::NEAREST_NEIGHBOR, arm_compute::S32, ScaleKernelInfo::sampling_policy, arm_compute::test::validation::shape, arm_compute::UNKNOWN, CpuScale::validate(), and arm_compute::WIDTH.

91 {
95 
96  _scale_info = info;
97  _is_prepared = false;
98 
99  // Get data layout and width/height indices
100  _data_layout = _scale_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : _scale_info.data_layout;
103 
104  // Compute the ratio between source width/height and destination width/height
105  const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
106  const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
107  const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
108 
109  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
110  InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
111  && hr <= 1.f) ?
113  _scale_info.interpolation_policy;
114 
115  // Get the tensor shape
116  TensorShape shape(dst->dimension(idx_width));
117  shape.set(1, dst->dimension(idx_height), false);
118 
119  TensorInfo tensor_info_offsets(shape, Format::S32);
120  TensorInfo tensor_info_dxdy(shape, Format::F32);
121 
122  auto dx = std::make_unique<TensorInfo>(tensor_info_dxdy);
123  auto dy = std::make_unique<TensorInfo>(tensor_info_dxdy);
124  auto offsets = std::make_unique<TensorInfo>(tensor_info_offsets);
125  auto scale_kernel = std::make_unique<kernels::CpuScaleKernel>();
126  switch(policy_to_use)
127  {
129  {
130  scale_kernel->configure(src, nullptr, nullptr, offsets.get(), dst, info);
131  break;
132  }
134  {
135  scale_kernel->configure(src, dx.get(), dy.get(), offsets.get(), dst, info);
136  break;
137  }
139  {
140  scale_kernel->configure(src, nullptr, nullptr, nullptr, dst, info);
141  break;
142  }
143  default:
144  ARM_COMPUTE_ERROR("Unsupported interpolation mode");
145  }
146  _kernel = std::move(scale_kernel);
147 }
InterpolationPolicy
Interpolation method.
Definition: Types.h:411
InterpolationPolicy interpolation_policy
Interpolation type to use.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 F32 per channel
Output values are defined by bilinear interpolation between the pixels.
bool align_corners
Align corners of input and output.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Output values are defined to match the source pixel whose center is nearest to the sample position...
SimpleTensor< float > src
Definition: DFT.cpp:155
bool is_align_corners_allowed_sampling_policy(SamplingPolicy sampling_policy)
Returns if aligned corners are allowed for the given sampling policy.
Definition: ScaleUtils.h:52
1 channel, 1 S32 per channel
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
Static function to check if given info will lead to a valid configuration.
Definition: CpuScale.cpp:149
SamplingPolicy sampling_policy
Sampling policy used by the interpolation.
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
#define ARM_COMPUTE_LOG_PARAMS(...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
float calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners=false)
Returns resize ratio between input and output with consideration of aligned corners.
Definition: ScaleUtils.cpp:27
DataLayout data_layout
Data layout to use.

◆ prepare()

void prepare ( ITensorPack constants)
overridevirtual

Prepare the function for executing.

Any one off pre-processing step required by the function is handled here

Parameters
[in]constantsVector that contains the constants tensors.
Note
Prepare stage might not need all the function's buffers' backing memory to be available in order to execute

Reimplemented from INEOperator.

Definition at line 194 of file CpuScale.cpp.

References arm_compute::ACL_DST, arm_compute::ACL_INT_0, arm_compute::ACL_INT_1, arm_compute::ACL_INT_2, arm_compute::ACL_SRC, ScaleKernelInfo::align_corners, arm_compute::AREA, ARM_COMPUTE_ERROR, arm_compute::BILINEAR, arm_compute::scale_utils::calculate_resize_ratio(), arm_compute::test::validation::dst, ITensorPack::get_const_tensor(), arm_compute::get_data_layout_dimension_index(), ITensorPack::get_tensor(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, arm_compute::test::validation::idx_width, ScaleKernelInfo::interpolation_policy, arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(), arm_compute::NEAREST_NEIGHBOR, ScaleKernelInfo::sampling_policy, arm_compute::test::validation::src, and arm_compute::WIDTH.

Referenced by CpuScale::run().

195 {
196  if(!_is_prepared)
197  {
198  _is_prepared = true;
199  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
200  auto dst = tensors.get_tensor(TensorType::ACL_DST);
201  auto dx = tensors.get_tensor(TensorType::ACL_INT_0);
202  auto dy = tensors.get_tensor(TensorType::ACL_INT_1);
203  auto offsets = tensors.get_tensor(TensorType::ACL_INT_2);
204 
205  // Get data layout and width/height indices
208 
209  // Compute the ratio between source width/height and destination width/height
210  const bool is_align_corners_used = _scale_info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(_scale_info.sampling_policy);
211  const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_width), dst->info()->dimension(idx_width), is_align_corners_used);
212  const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), is_align_corners_used);
213 
214  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
215  InterpolationPolicy policy_to_use = (_scale_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f
216  && hr <= 1.f) ?
218  _scale_info.interpolation_policy;
219  const SamplingPolicy sampling_policy = _scale_info.sampling_policy;
220 
221  switch(policy_to_use)
222  {
224  {
225  // Pre-compute offsets for nearest interpolation
226  precompute_dx_dy_offsets(nullptr, nullptr, offsets, wr, hr, sampling_policy, is_align_corners_used);
227  break;
228  }
230  {
231  // Pre-compute dx, dy and offsets for bilinear interpolation
232  precompute_dx_dy_offsets(dx, dy, offsets, wr, hr, sampling_policy, is_align_corners_used);
233  break;
234  }
236  {
237  break;
238  }
239  default:
240  ARM_COMPUTE_ERROR("Unsupported interpolation mode");
241  }
242  }
243 }
InterpolationPolicy
Interpolation method.
Definition: Types.h:411
InterpolationPolicy interpolation_policy
Interpolation type to use.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Output values are defined by bilinear interpolation between the pixels.
bool align_corners
Align corners of input and output.
Output values are defined to match the source pixel whose center is nearest to the sample position...
SimpleTensor< float > src
Definition: DFT.cpp:155
bool is_align_corners_allowed_sampling_policy(SamplingPolicy sampling_policy)
Returns if aligned corners are allowed for the given sampling policy.
Definition: ScaleUtils.h:52
SamplingPolicy sampling_policy
Sampling policy used by the interpolation.
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
float calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners=false)
Returns resize ratio between input and output with consideration of aligned corners.
Definition: ScaleUtils.cpp:27
SamplingPolicy
Available Sampling Policies.
Definition: Types.h:104

◆ run()

void run ( ITensorPack tensors)
overridevirtual

Run the kernels contained in the function.

Parameters
[in]tensorsVector that contains the tensors to operate on.

Reimplemented from INEOperator.

Definition at line 245 of file CpuScale.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, Window::DimY, ITensorPack::empty(), Scheduler::get(), CpuScale::prepare(), and IScheduler::schedule_op().

246 {
247  ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
248  prepare(tensors);
249  NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
250 }
virtual void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)=0
Runs the kernel in the same thread as the caller synchronously.
void prepare(ITensorPack &tensors) override
Prepare the function for executing.
Definition: CpuScale.cpp:194
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94

◆ validate()

Status validate ( const ITensorInfo src,
const ITensorInfo dst,
const ScaleKernelInfo info 
)
static

Static function to check if given info will lead to a valid configuration.

Similar to CpuScale::configure()

Returns
a status

Definition at line 149 of file CpuScale.cpp.

References ScaleKernelInfo::align_corners, arm_compute::AREA, ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::BILINEAR, arm_compute::scale_utils::calculate_resize_ratio(), arm_compute::CENTER, ICloneable< T >::clone(), ScaleKernelInfo::data_layout, ITensorInfo::data_layout(), arm_compute::test::validation::data_layout, ITensorInfo::dimension(), arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::test::validation::idx_height, arm_compute::test::validation::idx_width, arm_compute::test::validation::info, ScaleKernelInfo::interpolation_policy, arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(), arm_compute::NEAREST_NEIGHBOR, arm_compute::S32, ScaleKernelInfo::sampling_policy, arm_compute::test::validation::shape, arm_compute::TOP_LEFT, arm_compute::UNKNOWN, CpuScaleKernel::validate(), and arm_compute::WIDTH.

Referenced by CpuScale::configure(), and NEScale::validate().

150 {
153 
154  ITensorInfo *offsets = nullptr;
155  ITensorInfo *dx = nullptr;
156  ITensorInfo *dy = nullptr;
157 
158  // Get data layout and width/height indices
162 
163  // Compute the ratio between source width/height and destination width/height
165  const auto wr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), is_align_corners_used);
166  const auto hr = arm_compute::scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), is_align_corners_used);
167 
168  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
170 
171  // Get the tensor shape of auxilary buffers
172  const TensorShape shape(dst->dimension(idx_width), dst->dimension(idx_height));
173  TensorInfo tensor_info_offsets(shape, Format::S32);
174  TensorInfo tensor_info_dx(shape, Format::F32);
175  TensorInfo tensor_info_dy(shape, Format::F32);
176  switch(policy_to_use)
177  {
179  offsets = &tensor_info_offsets;
180  break;
182  offsets = &tensor_info_offsets;
183  dx = &tensor_info_dx;
184  dy = &tensor_info_dy;
185  break;
186  default:
187  break;
188  }
189 
190  ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuScaleKernel::validate(src->clone().get(), dx, dy, offsets, dst->clone().get(), info));
191  return Status{};
192 }
InterpolationPolicy
Interpolation method.
Definition: Types.h:411
InterpolationPolicy interpolation_policy
Interpolation type to use.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
1 channel, 1 F32 per channel
Output values are defined by bilinear interpolation between the pixels.
bool align_corners
Align corners of input and output.
Output values are defined to match the source pixel whose center is nearest to the sample position...
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Samples are taken at pixel center.
bool is_align_corners_allowed_sampling_policy(SamplingPolicy sampling_policy)
Returns if aligned corners are allowed for the given sampling policy.
Definition: ScaleUtils.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
1 channel, 1 S32 per channel
SamplingPolicy sampling_policy
Sampling policy used by the interpolation.
Samples are taken at pixel top left corner.
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
static Status validate(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Static function to check if given info will lead to a valid configuration.
float calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners=false)
Returns resize ratio between input and output with consideration of aligned corners.
Definition: ScaleUtils.cpp:27
DataLayout data_layout
Data layout to use.
DataLayout
[DataLayout enum definition]
Definition: Types.h:113

The documentation for this class was generated from the following files: