MCPcopy Create free account
hub / github.com/NVIDIA/DALI / SetupAndRun

Function SetupAndRun

dali/python/backend_impl.cc:2932–3029  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2930}
2931
2932void SetupAndRun(OperatorBase &self, Workspace &ws, std::optional<int> batch_size) {
2933 DomainTimeRange setup_and_run_tr("SetupAndRun " + GetOpDisplayName(self.GetSpec(), true),
2934 kDynamicDefaultColor);
2935 std::vector<dali::OutputDesc> out_descs;
2936 const auto &spec = self.GetSpec();
2937 if (ws.NumOutput() != 0)
2938 throw std::runtime_error("Workspace already has outputs defined");
2939
2940 auto AdjustLayout = [&](int idx, const TensorLayout &layout, auto backend) {
2941 using Backend = decltype(backend);
2942 const auto &inp = ws.Input<Backend>(idx);
2943 auto tl = std::make_shared<TensorList<Backend>>();
2944 tl->ShareData(inp);
2945 tl->SetLayout(layout);
2946 ws.SetInput(idx, std::move(tl));
2947 };
2948
2949 auto &schema = spec.GetSchemaOrDefault();
2950 for (int i = 0; i < spec.NumRegularInput(); i++) {
2951 auto layout = ws.GetInputLayout(i);
2952 auto ndim = ws.GetInputDim(i);
2953 auto adjusted_layout = schema.GetInputLayout(i, ndim, layout);
2954 if (layout != adjusted_layout) {
2955 if (ws.InputIsType<GPUBackend>(i)) {
2956 AdjustLayout(i, adjusted_layout, GPUBackend());
2957 } else {
2958 AdjustLayout(i, adjusted_layout, CPUBackend());
2959 }
2960 }
2961 }
2962
2963 bool pinned = ws.has_stream();
2964 std::optional<int> dev_id;
2965 if (ws.output_order().is_device())
2966 dev_id = ws.output_order().device_id();
2967
2968 for (int i = 0; i < spec.NumOutput(); i++) {
2969 if (spec.OutputDevice(i) == StorageDevice::CPU) {
2970 auto out = std::make_shared<TensorList<CPUBackend>>();
2971 if (dev_id.has_value())
2972 out->set_device_id(*dev_id);
2973 out->set_order(AccessOrder::host(), false); // make sure outputs are allocated in host order
2974 out->set_pinned(pinned);
2975 ws.AddOutput(std::move(out));
2976 } else {
2977 auto out = std::make_shared<TensorList<GPUBackend>>();
2978 if (!dev_id.has_value()) {
2979 throw std::logic_error(
2980 "Internal error: Workspace for a GPU operator doesn't have a valid device_id.");
2981 }
2982 out->set_device_id(*dev_id);
2983 out->set_order(ws.output_order(), false);
2984 ws.AddOutput(std::move(out));
2985 }
2986 }
2987
2988 if (batch_size.has_value()) {
2989 ws.SetBatchSizes(batch_size.value());

Callers 1

ExposeOperatorFunction · 0.85

Calls 15

GetOpDisplayNameFunction · 0.85
SetInputMethod · 0.80
GetInputDimMethod · 0.80
has_streamMethod · 0.80
output_orderMethod · 0.80
set_device_idMethod · 0.80
set_orderMethod · 0.80
set_pinnedMethod · 0.80
AddOutputMethod · 0.80
SetBatchSizesMethod · 0.80
GetRequestedBatchSizeMethod · 0.80
NumInputMethod · 0.80

Tested by

no test coverage detected