ipu_strategy.cc 27.5 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

A
Allen Guo 已提交
15
#include "paddle/fluid/platform/device/ipu/ipu_strategy.h"
A
Allen Guo 已提交
16 17 18 19 20 21

namespace {

template <typename Value, typename Lambda>
void RegisterSetter(
    std::map<std::string, std::function<void(Value)>>& options,  // NOLINT
22 23
    const std::string& name,
    Lambda setter) {
A
Allen Guo 已提交
24 25 26 27 28 29 30
  options[name] = setter;
}

template <typename Value, typename Lambda>
void RegisterGetter(
    std::map<std::string, std::function<Value()>>& options,  // NOLINT
    std::map<std::string, std::string>& options_type,        // NOLINT
31 32 33
    const std::string& name,
    const std::string& type_str,
    Lambda getter) {
A
Allen Guo 已提交
34 35 36 37
  options[name] = getter;
  options_type[name] = type_str;
}

38 39 40 41 42 43 44 45 46 47 48 49 50 51
struct DefaultCompilationProgressLogger {
  void operator()(int progress, int total) {
    if (progress != progress_ && progress % log_interval_ == 0) {
      progress_ = progress;
      VLOG(1) << "Graph compile progress: " << progress << "%";
    }
  }

  int log_interval_ = 10;
  int progress_ = 0;
  // default total progress
  int total_ = 100;
};

A
Allen Guo 已提交
52
}  // namespace
J
jianghaicheng 已提交
53 54 55

namespace paddle {
namespace platform {
A
Allen Guo 已提交
56 57
namespace ipu {

A
Allen Guo 已提交
58 59 60
IpuStrategy::IpuStrategy() {
#define ADD_BOOL_OPTION(name)                                             \
  RegisterSetter(bool_options, #name, [&](bool value) { name = value; }); \
61 62 63
  RegisterGetter(options_getter, options_type, #name, "bool", [&]() {     \
    return std::to_string(name);                                          \
  })
A
Allen Guo 已提交
64

65 66 67 68 69 70
#define ADD_UINT64_OPTION(name)                                           \
  RegisterSetter(                                                         \
      uint64_options, #name, [&](std::uint64_t value) { name = value; }); \
  RegisterGetter(options_getter, options_type, #name, "uint64", [&]() {   \
    return std::to_string(name);                                          \
  })
A
Allen Guo 已提交
71 72 73

#define ADD_DOUBLE_OPTION(name)                                               \
  RegisterSetter(double_options, #name, [&](double value) { name = value; }); \
74 75 76
  RegisterGetter(options_getter, options_type, #name, "double", [&]() {       \
    return std::to_string(name);                                              \
  })
A
Allen Guo 已提交
77

78 79 80 81 82
#define ADD_STRING_OPTION(name)                                                \
  RegisterSetter(                                                              \
      string_options, #name, [&](const std::string& value) { name = value; }); \
  RegisterGetter(                                                              \
      options_getter, options_type, #name, "string", [&]() { return name; })
A
Allen Guo 已提交
83 84 85 86

  ADD_BOOL_OPTION(is_training);
  ADD_BOOL_OPTION(need_avg_shard);
  ADD_BOOL_OPTION(enable_fp16);
A
Allen Guo 已提交
87 88
  ADD_BOOL_OPTION(use_no_bias_optimizer);
  ADD_BOOL_OPTION(enable_distribution);
A
Allen Guo 已提交
89
  ADD_BOOL_OPTION(scaled_optimizer_state);
A
Allen Guo 已提交
90 91 92
  ADD_UINT64_OPTION(num_ipus);
  ADD_UINT64_OPTION(batches_per_step);
  ADD_UINT64_OPTION(micro_batch_size);
A
Allen Guo 已提交
93
  ADD_UINT64_OPTION(random_seed);
94
  ADD_UINT64_OPTION(tiles_per_ipu);
A
Allen Guo 已提交
95 96 97
  ADD_DOUBLE_OPTION(available_memory_proportion);
  ADD_DOUBLE_OPTION(loss_scaling);
  ADD_DOUBLE_OPTION(max_weight_norm);
A
Allen Guo 已提交
98 99 100 101 102
  ADD_STRING_OPTION(accl1_type);
  ADD_STRING_OPTION(accl2_type);
  ADD_STRING_OPTION(accl3_type);
  ADD_STRING_OPTION(onnx_dump_path);
  ADD_STRING_OPTION(weight_decay_mode);
A
Allen Guo 已提交
103 104 105 106 107 108

#undef ADD_STRING_OPTION
#undef ADD_DOUBLE_OPTION
#undef ADD_UINT64_OPTION
#undef ADD_BOOL_OPTION

109 110 111 112 113 114
#define ADD_RUNTIME_BOOL_OPTION(name, aliased_name)                   \
  RegisterSetter(bool_options, #name, [&](bool value) {               \
    runtime_options.aliased_name = value;                             \
  });                                                                 \
  RegisterGetter(options_getter, options_type, #name, "bool", [&]() { \
    return std::to_string(runtime_options.aliased_name);              \
A
Allen Guo 已提交
115 116 117 118 119 120
  })

  ADD_RUNTIME_BOOL_OPTION(runtime_options.enable_eval, enable_eval);

#undef ADD_RUNTIME_BOOL_OPTION

A
Allen Guo 已提交
121 122 123
#define ADD_POPART_ENUM_OPTION_ALIAS(name, aliased_name, EnumType)        \
  RegisterSetter(uint64_options, #name, [&](std::uint64_t value) {        \
    PADDLE_ENFORCE_LT(                                                    \
124 125
        value,                                                            \
        static_cast<std::uint64_t>(popart::EnumType::N),                  \
A
Allen Guo 已提交
126 127 128 129 130 131 132 133
        errors::InvalidArgument("Value for %s out of range", #EnumType)); \
    popart_options.aliased_name = static_cast<popart::EnumType>(value);   \
  });                                                                     \
  RegisterGetter(options_getter, options_type, #name, "uint64", [&]() {   \
    return std::to_string(                                                \
        static_cast<std::uint64_t>(popart_options.aliased_name));         \
  })

134 135 136 137 138 139
#define ADD_POPART_BOOL_OPTION_ALIAS(name, aliased_name)              \
  RegisterSetter(bool_options, #name, [&](bool value) {               \
    popart_options.aliased_name = value;                              \
  });                                                                 \
  RegisterGetter(options_getter, options_type, #name, "bool", [&]() { \
    return std::to_string(popart_options.aliased_name);               \
A
Allen Guo 已提交
140 141 142 143 144 145 146 147 148 149
  })

#define ADD_POPART_UINT64_OPTION_ALIAS(name, aliased_name)              \
  RegisterSetter(uint64_options, #name, [&](std::uint64_t value) {      \
    popart_options.aliased_name = value;                                \
  });                                                                   \
  RegisterGetter(options_getter, options_type, #name, "uint64", [&]() { \
    return std::to_string(popart_options.aliased_name);                 \
  })

150 151 152 153 154 155
#define ADD_POPART_DOUBLE_OPTION_ALIAS(name, aliased_name)              \
  RegisterSetter(double_options, #name, [&](double value) {             \
    popart_options.aliased_name = value;                                \
  });                                                                   \
  RegisterGetter(options_getter, options_type, #name, "double", [&]() { \
    return std::to_string(popart_options.aliased_name);                 \
A
Allen Guo 已提交
156 157 158 159 160 161
  })

#define ADD_POPART_STRING_OPTION_ALIAS(name, aliased_name)              \
  RegisterSetter(string_options, #name, [&](const std::string& value) { \
    popart_options.aliased_name = value;                                \
  });                                                                   \
162 163 164
  RegisterGetter(options_getter, options_type, #name, "string", [&]() { \
    return popart_options.aliased_name;                                 \
  })
A
Allen Guo 已提交
165

166 167 168 169 170 171 172 173 174 175 176 177
  ADD_POPART_ENUM_OPTION_ALIAS(autodiff_settings.stitch_strategy,
                               autodiffSettings.stitchStrategy,
                               AutodiffStitchStrategy);
  ADD_POPART_ENUM_OPTION_ALIAS(batch_serialization_settings.transform_context,
                               batchSerializationSettings.transformContext,
                               BatchSerializationTransformContext);
  ADD_POPART_ENUM_OPTION_ALIAS(batch_serialization_settings.method,
                               batchSerializationSettings.method,
                               BatchSerializationMethod);
  ADD_POPART_ENUM_OPTION_ALIAS(batch_serialization_settings.batch_schedule,
                               batchSerializationSettings.batchSchedule,
                               BatchSerializationBatchSchedule);
178 179 180 181 182 183 184 185
  ADD_POPART_ENUM_OPTION_ALIAS(
      auto_recomputation, autoRecomputation, RecomputationType);
  ADD_POPART_ENUM_OPTION_ALIAS(
      merge_var_update, mergeVarUpdate, MergeVarUpdateType);
  ADD_POPART_ENUM_OPTION_ALIAS(
      virtual_graph_mode, virtualGraphMode, VirtualGraphMode);
  ADD_POPART_ENUM_OPTION_ALIAS(
      synthetic_data_mode, syntheticDataMode, SyntheticDataMode);
186 187 188 189 190 191 192 193
  ADD_POPART_ENUM_OPTION_ALIAS(subgraph_copying_strategy,
                               subgraphCopyingStrategy,
                               SubgraphCopyingStrategy);
  ADD_POPART_ENUM_OPTION_ALIAS(accumulation_and_replication_reduction_type,
                               accumulationAndReplicationReductionType,
                               ReductionType);
  ADD_POPART_ENUM_OPTION_ALIAS(
      mean_accumulation_and_replication_reduction_strategy,
194 195
      meanAccumulationAndReplicationReductionStrategy,
      MeanReductionStrategy);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

  ADD_POPART_STRING_OPTION_ALIAS(log_dir, logDir);
  ADD_POPART_STRING_OPTION_ALIAS(cache_path, cachePath);
  ADD_POPART_STRING_OPTION_ALIAS(partials_type_matmuls, partialsTypeMatMuls);
  ADD_POPART_STRING_OPTION_ALIAS(custom_codelet_compile_flags,
                                 customCodeletCompileFlags);
  ADD_POPART_STRING_OPTION_ALIAS(serialized_poprithms_shift_graphs_dir,
                                 serializedPoprithmsShiftGraphsDir);
  ADD_POPART_STRING_OPTION_ALIAS(kahn_tie_breaker, kahnTieBreaker);

  ADD_POPART_UINT64_OPTION_ALIAS(execution_phase_settings.phases,
                                 executionPhaseSettings.phases);
  ADD_POPART_UINT64_OPTION_ALIAS(execution_phase_settings.stages,
                                 executionPhaseSettings.stages);
  ADD_POPART_UINT64_OPTION_ALIAS(batch_serialization_settings.factor,
                                 batchSerializationSettings.factor);
  ADD_POPART_UINT64_OPTION_ALIAS(first_dot_op, firstDotOp);
  ADD_POPART_UINT64_OPTION_ALIAS(final_dot_op, finalDotOp);
  ADD_POPART_UINT64_OPTION_ALIAS(num_io_tiles, numIOTiles);
  ADD_POPART_UINT64_OPTION_ALIAS(merge_var_update_mem_threshold,
                                 mergeVarUpdateMemThreshold);
  ADD_POPART_UINT64_OPTION_ALIAS(loose_threshold_at_peak, looseThresholdAtPeak);
A
Allen Guo 已提交
218
  ADD_POPART_UINT64_OPTION_ALIAS(replicated_graph_count, replicatedGraphCount);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  ADD_POPART_UINT64_OPTION_ALIAS(accumulation_factor, accumulationFactor);
  ADD_POPART_UINT64_OPTION_ALIAS(swap_limit_scheduler, swapLimitScheduler);
  ADD_POPART_UINT64_OPTION_ALIAS(global_replication_factor,
                                 globalReplicationFactor);
  ADD_POPART_UINT64_OPTION_ALIAS(global_replica_offset, globalReplicaOffset);
  ADD_POPART_UINT64_OPTION_ALIAS(default_prefetch_buffering_depth,
                                 defaultPrefetchBufferingDepth);
  ADD_POPART_UINT64_OPTION_ALIAS(compilation_progress_total,
                                 compilationProgressTotal);
  ADD_POPART_UINT64_OPTION_ALIAS(transitive_closure_optimization_threshold,
                                 transitiveClosureOptimizationThreshold);

  ADD_POPART_BOOL_OPTION_ALIAS(
      batch_serialization_settings.concat_on_virtual_graph_change,
      batchSerializationSettings.concatOnVirtualGraphChange);
  ADD_POPART_BOOL_OPTION_ALIAS(
      batch_serialization_settings.concat_on_execution_phase_change,
A
Allen Guo 已提交
236
      batchSerializationSettings.concatOnExecutionPhaseChange);
237 238
  ADD_POPART_BOOL_OPTION_ALIAS(
      batch_serialization_settings.concat_on_pipeline_stage_change,
A
Allen Guo 已提交
239
      batchSerializationSettings.concatOnPipelineStageChange);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  ADD_POPART_BOOL_OPTION_ALIAS(strict_op_versions, strictOpVersions);
  ADD_POPART_BOOL_OPTION_ALIAS(opx_alias_checking, opxAliasChecking);
  ADD_POPART_BOOL_OPTION_ALIAS(opx_modify_checking, opxModifyChecking);
  ADD_POPART_BOOL_OPTION_ALIAS(dot_op_names, dotOpNames);
  ADD_POPART_BOOL_OPTION_ALIAS(export_poplar_computation_graph,
                               exportPoplarComputationGraph);
  ADD_POPART_BOOL_OPTION_ALIAS(export_poplar_vertex_graph,
                               exportPoplarVertexGraph);
  ADD_POPART_BOOL_OPTION_ALIAS(separate_call_op_pdfs, separateCallOpPdfs);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_outlining, enableOutlining);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_outlining_copy_cost_pruning,
                               enableOutliningCopyCostPruning);
  ADD_POPART_BOOL_OPTION_ALIAS(rearrange_anchors_on_host,
                               rearrangeAnchorsOnHost);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_prefetch_datastreams,
                               enablePrefetchDatastreams);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_non_stable_softmax,
                               enableNonStableSoftmax);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_replicated_graphs,
                               enableReplicatedGraphs);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_gradient_accumulation,
                               enableGradientAccumulation);
  ADD_POPART_BOOL_OPTION_ALIAS(instrument_with_hardware_cycle_counter,
                               instrumentWithHardwareCycleCounter);
A
Allen Guo 已提交
264
  ADD_POPART_BOOL_OPTION_ALIAS(enable_pipelining, enablePipelining);
265 266 267 268 269 270 271 272
  ADD_POPART_BOOL_OPTION_ALIAS(disable_grad_accumulation_tensor_streams,
                               disableGradAccumulationTensorStreams);
  ADD_POPART_BOOL_OPTION_ALIAS(compile_engine, compileEngine);
  ADD_POPART_BOOL_OPTION_ALIAS(constant_weights, constantWeights);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_engine_caching, enableEngineCaching);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_merge_exchange, enableMergeExchange);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_floating_point_checks,
                               enableFloatingPointChecks);
A
Allen Guo 已提交
273 274
  ADD_POPART_BOOL_OPTION_ALIAS(enable_stochastic_rounding,
                               enableStochasticRounding);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  ADD_POPART_BOOL_OPTION_ALIAS(explicit_recomputation, explicitRecomputation);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_explicit_main_loops,
                               enableExplicitMainLoops);
  ADD_POPART_BOOL_OPTION_ALIAS(use_host_copy_ops, useHostCopyOps);
  ADD_POPART_BOOL_OPTION_ALIAS(alias_zero_copy, aliasZeroCopy);
  ADD_POPART_BOOL_OPTION_ALIAS(delay_var_updates, delayVarUpdates);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_fully_connected_pass,
                               enableFullyConnectedPass);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_serialized_matmuls,
                               enableSerializedMatmuls);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_stable_norm, enableStableNorm);
  ADD_POPART_BOOL_OPTION_ALIAS(decompose_grad_sum, decomposeGradSum);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_distributed_replicated_graphs,
                               enableDistributedReplicatedGraphs);
  ADD_POPART_BOOL_OPTION_ALIAS(group_host_sync, groupHostSync);
  ADD_POPART_BOOL_OPTION_ALIAS(automatic_loss_scaling_settings.enabled,
                               automaticLossScalingSettings.enabled);
  ADD_POPART_BOOL_OPTION_ALIAS(instrument_with_hardware_cycle_counter,
                               instrumentWithHardwareCycleCounter);
  ADD_POPART_BOOL_OPTION_ALIAS(enable_supported_data_type_casting,
                               enableSupportedDataTypeCasting);
  ADD_POPART_BOOL_OPTION_ALIAS(group_norm_strided_channel_grouping,
                               groupNormStridedChannelGrouping);
  ADD_POPART_BOOL_OPTION_ALIAS(
      schedule_non_weight_update_gradient_consumers_early,
      scheduleNonWeightUpdateGradientConsumersEarly);
301 302
  ADD_POPART_BOOL_OPTION_ALIAS(create_implicit_pipelining_fwd_only_program,
                               createImplicitPipeliningFwdOnlyProgram);
303 304 305 306 307 308 309 310 311 312

  ADD_POPART_DOUBLE_OPTION_ALIAS(outline_sequence_break_cost,
                                 outlineSequenceBreakCost);
  ADD_POPART_DOUBLE_OPTION_ALIAS(outline_threshold, outlineThreshold);
  ADD_POPART_DOUBLE_OPTION_ALIAS(time_limit_scheduler, timeLimitScheduler);
  ADD_POPART_DOUBLE_OPTION_ALIAS(
      automatic_loss_scaling_settings.bin_edge_location,
      automaticLossScalingSettings.binEdgeLocation);
  ADD_POPART_DOUBLE_OPTION_ALIAS(
      automatic_loss_scaling_settings.threshold_upper_count_proportion,
A
Allen Guo 已提交
313 314 315 316 317 318 319 320
      automaticLossScalingSettings.thresholdUpperCountProportion);

#undef ADD_POPART_STRING_OPTION_ALIAS
#undef ADD_POPART_DOUBLE_OPTION_ALIAS
#undef ADD_POPART_UINT64_OPTION_ALIAS
#undef ADD_POPART_BOOL_OPTION_ALIAS
#undef ADD_POPART_ENUM_OPTION_ALIAS

321 322 323 324 325 326 327 328
  RegisterGetter(
      vector_options_getter, options_type, "custom_ops", "vector", [&]() {
        std::vector<std::string> res;
        for (auto x : custom_ops) {
          res.push_back(x.repr());
        }
        return res;
      });
A
Allen Guo 已提交
329

A
Allen Guo 已提交
330 331 332 333 334 335 336 337
  RegisterSetter(bool_options, "enable_manual_shard", [&](bool value) {
    if (value) {
      popart_options.virtualGraphMode = popart::VirtualGraphMode::Manual;
    } else {
      popart_options.virtualGraphMode = popart::VirtualGraphMode::Off;
    }
  });

338 339 340 341 342
  RegisterGetter(
      options_getter, options_type, "enable_manual_shard", "bool", [&]() {
        return std::to_string(popart_options.virtualGraphMode ==
                              popart::VirtualGraphMode::Manual);
      });
A
Allen Guo 已提交
343 344 345 346

  RegisterSetter(bool_options, "enable_half_partial", [&](bool value) {
    if (value) {
      popart_options.partialsTypeMatMuls = "half";
347
      popart_options.convolutionOptions.insert({{"partialsType", "half"}});
A
Allen Guo 已提交
348 349
    } else {
      popart_options.partialsTypeMatMuls = "float";
350
      popart_options.convolutionOptions.insert({{"partialsType", "float"}});
A
Allen Guo 已提交
351 352 353 354 355 356 357 358
    }
  });

  RegisterGetter(
      options_getter, options_type, "enable_half_partial", "bool", [&]() {
        return std::to_string(popart_options.partialsTypeMatMuls == "half");
      });

359 360
  RegisterSetter(container_options,
                 "dot_checks",
361
                 [&](const std::pair<std::string, std::string>& p) {
362 363
                   std::vector<std::string> valid_dot{
                       "Fwd0", "Fwd1", "Bwd0", "PreAlias", "Final"};
364 365 366 367 368 369 370
                   if (std::find(valid_dot.begin(), valid_dot.end(), p.first) ==
                       valid_dot.end()) {
                     PADDLE_THROW(platform::errors::InvalidArgument(
                         "Unknown dot check: %s", p.first));
                   }
                   popart_options.dotChecks.insert(p.first);
                 });
A
Allen Guo 已提交
371

372 373 374 375 376 377 378 379
  RegisterGetter(
      vector_options_getter, options_type, "dot_checks", "vector", [&]() {
        std::vector<std::string> res;
        for (auto x : popart_options.dotChecks) {
          res.push_back(x);
        }
        return res;
      });
A
Allen Guo 已提交
380

381 382
  RegisterSetter(container_options,
                 "hardware_instrumentations",
A
Allen Guo 已提交
383 384 385 386 387 388 389
                 [&](const std::pair<std::string, std::string>& p) {
                   std::uint64_t value = std::stoul(p.first);
                   popart_options.hardwareInstrumentations.insert(
                       static_cast<popart::Instrumentation>(value));
                 });

  RegisterGetter(
390 391 392 393 394
      vector_options_getter,
      options_type,
      "hardware_instrumentations",
      "vector",
      [&]() {
A
Allen Guo 已提交
395 396 397 398 399 400 401
        std::vector<std::string> res;
        for (auto x : popart_options.hardwareInstrumentations) {
          res.push_back(std::to_string(static_cast<std::uint64_t>(x)));
        }
        return res;
      });

402 403
  RegisterSetter(container_options,
                 "custom_codelets",
A
Allen Guo 已提交
404 405 406 407
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.customCodelets.push_back(p.first);
                 });

408 409 410 411 412 413 414 415
  RegisterGetter(
      vector_options_getter, options_type, "custom_codelets", "vector", [&]() {
        std::vector<std::string> res;
        for (auto x : popart_options.customCodelets) {
          res.push_back(x);
        }
        return res;
      });
A
Allen Guo 已提交
416

417 418
  RegisterSetter(container_options,
                 "engine_options",
A
Allen Guo 已提交
419 420 421 422
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.engineOptions.emplace(p);
                 });

423 424 425 426
  RegisterGetter(
      map_options_getter, options_type, "engine_options", "map", [&]() {
        return popart_options.engineOptions;
      });
A
Allen Guo 已提交
427

428 429
  RegisterSetter(container_options,
                 "report_options",
A
Allen Guo 已提交
430 431 432 433
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.reportOptions.emplace(p);
                 });

434 435 436 437
  RegisterGetter(
      map_options_getter, options_type, "report_options", "map", [&]() {
        return popart_options.reportOptions;
      });
A
Allen Guo 已提交
438

439 440
  RegisterSetter(container_options,
                 "convolution_options",
A
Allen Guo 已提交
441 442 443 444
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.convolutionOptions.emplace(p);
                 });

445 446 447 448
  RegisterGetter(
      map_options_getter, options_type, "convolution_options", "map", [&]() {
        return popart_options.convolutionOptions;
      });
A
Allen Guo 已提交
449

450 451
  RegisterSetter(container_options,
                 "lstm_options",
A
Allen Guo 已提交
452 453 454 455
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.lstmOptions.emplace(p);
                 });

456 457 458 459
  RegisterGetter(
      map_options_getter, options_type, "lstm_options", "map", [&]() {
        return popart_options.lstmOptions;
      });
A
Allen Guo 已提交
460

461 462
  RegisterSetter(container_options,
                 "gcl_options",
A
Allen Guo 已提交
463 464 465 466
                 [&](const std::pair<std::string, std::string>& p) {
                   popart_options.gclOptions.emplace(p);
                 });

467 468 469
  RegisterGetter(map_options_getter, options_type, "gcl_options", "map", [&]() {
    return popart_options.gclOptions;
  });
A
Allen Guo 已提交
470 471 472 473

  // Default options

  // Can also be set as a custom logger in python, like using tqdm
474
  popart_options.compilationProgressLogger = DefaultCompilationProgressLogger();
A
Allen Guo 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
}

void IpuStrategy::AddBoolOption(const std::string& option, bool value) {
  set(option, value, bool_options, "bool");
}

void IpuStrategy::AddUint64Option(const std::string& option,
                                  std::uint64_t value) {
  set(option, value, uint64_options, "uint64");
}

void IpuStrategy::AddDoubleOption(const std::string& option, double value) {
  set(option, value, double_options, "double");
}

void IpuStrategy::AddStringOption(const std::string& option,
                                  const std::string& value) {
  set(option, value, string_options, "string");
}

void IpuStrategy::InsertStringOption(const std::string& option,
                                     const std::string& value) {
497 498 499
  set(option,
      std::pair<std::string, std::string>(value, ""),
      container_options,
A
Allen Guo 已提交
500 501 502 503 504 505
      "vector");
}

void IpuStrategy::InsertStringPairOption(const std::string& option,
                                         const std::string& key,
                                         const std::string& value) {
506 507 508 509
  set(option,
      std::pair<std::string, std::string>(key, value),
      container_options,
      "map");
A
Allen Guo 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
}

void IpuStrategy::SetTensorLocation(const std::string& tensor,
                                    const std::string& opt,
                                    std::uint64_t value) {
  VLOG(10) << "Setting " << opt << " to " << value << " for location "
           << tensor;
  popart::TensorLocationSettings* settings;
  if (tensor == "location_activation") {
    settings = &popart_options.activationTensorLocationSettings;
  } else if (tensor == "location_weight") {
    settings = &popart_options.weightTensorLocationSettings;
  } else if (tensor == "location_optimizer") {
    settings = &popart_options.optimizerStateTensorLocationSettings;
  } else if (tensor == "location_accumulator") {
    settings = &popart_options.accumulatorTensorLocationSettings;
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Unknown tensor location: %s", tensor));
  }

531
  if (opt == "min_elements_for_off_chip") {
A
Allen Guo 已提交
532
    settings->minElementsForOffChip = value;
533
  } else if (opt == "min_elements_for_replicated_tensor_sharding") {
A
Allen Guo 已提交
534
    settings->minElementsForReplicatedTensorSharding = value;
535
  } else if (opt == "on_chip") {
A
Allen Guo 已提交
536 537
    settings->location.storage = value > 0 ? popart::TensorStorage::OnChip
                                           : popart::TensorStorage::OffChip;
538
  } else if (opt == "use_replicated_tensor_sharding") {
A
Allen Guo 已提交
539 540 541
    settings->location.replicatedTensorSharding =
        value > 0 ? popart::ReplicatedTensorSharding::On
                  : popart::ReplicatedTensorSharding::Off;
542
  } else if (opt == "use_io_tiles_to_load") {
A
Allen Guo 已提交
543 544
    settings->location.loadTileSet =
        value > 0 ? popart::TileSet::IO : popart::TileSet::Compute;
545
  } else if (opt == "use_io_tiles_to_store") {
A
Allen Guo 已提交
546 547
    settings->location.storageTileSet =
        value > 0 ? popart::TileSet::IO : popart::TileSet::Compute;
A
Allen Guo 已提交
548 549 550 551 552 553 554 555 556
  } else if (opt == "sharding_domain_with_all") {
    settings->location.shardingDomain =
        popart::CommGroup(popart::CommGroupType::All, value);
  } else if (opt == "sharding_domain_with_consecutive") {
    settings->location.shardingDomain =
        popart::CommGroup(popart::CommGroupType::Consecutive, value);
  } else if (opt == "sharding_domain_with_orthogonal") {
    settings->location.shardingDomain =
        popart::CommGroup(popart::CommGroupType::Orthogonal, value);
A
Allen Guo 已提交
557 558 559 560 561 562
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Unknown option ' %s' for tensor location: %s", opt, tensor));
  }
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
void IpuStrategy::SetReplicatedCollectivesSettings(const std::string& opt,
                                                   bool value) {
  VLOG(10) << "Set Replica Setting " << opt << " to " << value;
  if (opt == "prepare_schedule_for_merging_collectives") {
    popart_options.replicatedCollectivesSettings
        .prepareScheduleForMergingCollectives = value;
  } else if (opt == "merge_all_reduce_collectives") {
    popart_options.replicatedCollectivesSettings.mergeAllReduceCollectives =
        value;
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Unknown option ' %s' for replicated collectives settings", opt));
  }
}

A
Allen Guo 已提交
578 579 580 581 582 583 584 585 586
void IpuStrategy::SetAccumulateOuterFragmentSettings(
    const std::uint64_t& schedule, const std::vector<int>& values) {
  VLOG(10) << "SetAccumulateOuterFragmentSettings schedule:" << schedule;
  auto schedule_ =
      static_cast<popart::AccumulateOuterFragmentSchedule>(schedule);
  popart_options.accumulateOuterFragmentSettings =
      popart::AccumulateOuterFragmentSettings(schedule_, values);
}

A
Allen Guo 已提交
587 588
void IpuStrategy::AddCustomOp(const std::string& paddle_op,
                              const std::string& popart_op,
589 590
                              const std::string& domain,
                              int version) {
A
Allen Guo 已提交
591 592 593 594 595
  LOG(INFO) << "IpuStrategy add custom op: " << paddle_op;
  custom_ops.push_back(
      IpuCustomOpIdentifier(paddle_op, popart_op, domain, version));
}

A
Allen Guo 已提交
596 597 598 599 600
void IpuStrategy::SetCompilationProgressLogger(
    const std::function<void(int, int)>& logger) {
  popart_options.compilationProgressLogger = logger;
}

A
Allen Guo 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
std::string IpuStrategy::GetOption(const std::string& option) {
  return get(option, options_getter);
}

std::vector<std::string> IpuStrategy::GetVectorOption(
    const std::string& option) {
  return get(option, vector_options_getter);
}

std::map<std::string, std::string> IpuStrategy::GetMapOption(
    const std::string& option) {
  return get(option, map_options_getter);
}

std::string IpuStrategy::GetOptionType(const std::string& option) {
  return options_type[option];
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632
std::vector<std::string> IpuStrategy::GetAllOptionNames() {
  std::vector<std::string> names;
  for (auto& option : options_getter) {
    names.push_back(option.first);
  }
  for (auto& option : vector_options_getter) {
    names.push_back(option.first);
  }
  for (auto& option : map_options_getter) {
    names.push_back(option.first);
  }
  return names;
}

A
Allen Guo 已提交
633
void IpuStrategy::EnablePattern(const std::string& t) {
A
Allen Guo 已提交
634 635 636 637
  VLOG(10) << "enable popart pattern: " << t;
  popart_patterns.enablePattern(t, true);
}

A
Allen Guo 已提交
638
void IpuStrategy::DisablePattern(const std::string& t) {
A
Allen Guo 已提交
639 640 641 642
  VLOG(10) << "disable popart pattern: " << t;
  popart_patterns.enablePattern(t, false);
}

A
Allen Guo 已提交
643
const bool IpuStrategy::IsPatternEnabled(const std::string& t) {
A
Allen Guo 已提交
644 645 646 647
  return popart_patterns.isPatternEnabled(t);
}

}  // namespace ipu
J
jianghaicheng 已提交
648 649
}  // namespace platform
}  // namespace paddle