Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/optimizer/insert_cast_transformer.h"
#include "core/framework/data_types.h"
#include "core/framework/kernel_type_str_resolver.h"
#include "core/graph/graph_utils.h"
#include "core/framework/compute_capability.h"
#include "core/graph/indexed_sub_graph.h"
 
using namespace ONNX_NAMESPACE;
using namespace ::onnxruntime::common;
namespace onnxruntime {
static bool IsMLFloat16Tensor(const NodeArg& node_arg) {
  // Type() will return nullptr if node_arg.Exists() is true so don't need an additional check for that
  return node_arg.Type() != nullptr &&
         DataTypeImpl::TypeFromProto(*node_arg.TypeAsProto()) == DataTypeImpl::GetTensorType<MLFloat16>();
}
 
using KernelRegistryList = InsertCastTransformer::KernelRegistryList;
 
bool InsertCastTransformer::NeedInsertCast(const onnxruntime::Node* node, const onnxruntime::NodeArg* input) const {
  // If the node's input is float16 and currently the node is not assigned to any EP
  // we need to insert a cast to float, and put the node on CPU for default behavior.
  // We don't cast a node with a subgraph as we'd need to do a lot more checking of the subgraph inputs
  // (both explicit and implicit) and contents to determine if it was safe to do so.
  // TODO: a better check is to check does the CPU kernel with float exist or not.
  return node->GetExecutionProviderType().empty() &&
         !node->ContainsSubgraph() &&
         IsMLFloat16Tensor(*input);
}
 
onnxruntime::NodeArg* AddCastNode(onnxruntime::Graph& graph,
                                  onnxruntime::NodeArg* old_arg,
                                  TypeProto* new_type,
                                  bool new_on_input,
                                  int64_t to_type,
                                  onnxruntime::ProviderType providerType) {
  // insert cast op to cast input
  std::string node_name = graph.GenerateNodeName("InsertedPrecisionFreeCast_" + old_arg->Name());
 
  auto* new_arg = &graph.GetOrCreateNodeArg(node_name, new_type);
 
  std::vector<onnxruntime::NodeArg*> input_defs = {new_on_input ? new_arg : old_arg};
  std::vector<onnxruntime::NodeArg*> output_defs = {new_on_input ? old_arg : new_arg};
 
  auto& cast_node = graph.AddNode(node_name, "Cast", "cast node to cast from float16 to float32 on cpu",
                                  input_defs, output_defs);
  cast_node.AddAttribute("to", to_type);
  cast_node.SetExecutionProviderType(providerType);
  return new_arg;
}
 
// check if the node has an fp16 input but was not able to be assigned an execution provider.
// we will need to add casts to/from fp32 around the node for it to be executed using the CPU EP.
static bool NodeNeedsInputCastToFp32(const onnxruntime::Node& node) {
  bool not_assigned = node.GetExecutionProviderType().empty();
 
  if (not_assigned) {
    const auto& input_defs = node.InputDefs();
    bool has_fp16_input = std::any_of(input_defs.cbegin(), input_defs.cend(),
                                      [](const NodeArg* input_def) {
                                        return IsMLFloat16Tensor(*input_def);
                                      });
    return has_fp16_input;
  }
 
  return false;
}
 
// Detect a node that was assigned to the CPU EP but has no kernel for the types it actually uses.
//
// Graph transformers that run after partitioning (the Level2+ fusions) give a fused node the execution
// provider of the nodes it replaces without checking that a kernel for it exists. An fp16 Add and an
// fp16 Gelu both have CPU kernels and so are assigned to the CPU EP, but fusing them produces a
// com.microsoft.BiasGelu node which the CPU EP only implements for float. Such a node has to be
// converted to fp32 like an unassigned one, otherwise session initialization fails when its kernel is
// looked up.
static bool IsFp16NodeOnCpuWithoutKernel(const onnxruntime::Node& node,
                                         const KernelRegistryList& cpu_kernel_registries,
                                         const logging::Logger& logger) {
  if (node.GetExecutionProviderType() != kCpuExecutionProvider ||
      node.ContainsSubgraph() ||
      node.Op() == nullptr) {  // no schema, so the type constraints cannot be resolved
    return false;
  }
 
  const auto& input_defs = node.InputDefs();
  const auto& output_defs = node.OutputDefs();
  const auto is_fp16 = [](const NodeArg* def) { return IsMLFloat16Tensor(*def); };
  if (std::none_of(input_defs.cbegin(), input_defs.cend(), is_fp16) &&
      std::none_of(output_defs.cbegin(), output_defs.cend(), is_fp16)) {
    return false;
  }
 
  // Check every registry, not just the CPU EP's own one: a kernel registered through a custom registry
  // takes priority at kernel lookup time, so a node it covers is not kernel-less and must be left alone.
  const OpSchemaKernelTypeStrResolver kernel_type_str_resolver{};
  const KernelCreateInfo* kernel_create_info{};
  return std::none_of(cpu_kernel_registries.cbegin(), cpu_kernel_registries.cend(),
                      [&](const gsl::not_null<const KernelRegistry*>& registry) {
                        const auto lookup_status = registry->TryFindKernel(
                            node, kCpuExecutionProvider, kernel_type_str_resolver, logger, &kernel_create_info);
                        return lookup_status.IsOK();
                      });
}
 
// Detect an isolated node that is able to process fp16 data but is between other nodes that have fp16 inputs
// but will need a Cast inserted to enable them to run.
//
// Say we have 3 nodes in the middle of a graph that all have fp16 inputs.
//
// -> NodeA -> NodeB -> NodeC ->
//
// NodeA and NodeC have no kernel that can handle fp16 data (no execution provider assigned).
//   e.g. 'Add' does not have an fp16 kernel
// NodeB has a kernel that can process fp16 data (assigned to CPU EP).
//
// By default, we would insert Cast to/from fp32 around NodeA and NodeC as all operators have an fp32 kernel.
//
// i.e. -> CastToFp32 -> NodeA -> CastToFp16 -> NodeB -> CastToFp32 -> NodeC -> CastToFp16
//
// We can avoid the casts around NodeB if we also force that to run using fp32 data.
//
// Detect this scenario by checking the input and output edges of the node for fp16 values to that are coming from or
// going to a node that will need a Cast.
//
// Return true if all the fp16 inputs and outputs are connected to nodes that will be cast to fp32.
//
// If no_cpu_kernel is true the node cannot run as fp16 at all, so the checks on the neighbouring nodes
// are skipped: converting it to fp32 is not an optimization then but the only way to run it.
static bool IsIsolatedFp16NodeOnCpu(const onnxruntime::Node& node, onnxruntime::Graph& graph,
                                    const KernelRegistryList& cpu_kernel_registries,
                                    const logging::Logger& logger,
                                    bool no_cpu_kernel) {
  // we can check if it's an isolated fp16 node
  // if node has input coming from other nodes (only consuming graph inputs or initializers if it doesn't),
  //    does not have a subgraph (would have to alter subgraph inputs if we cast the input to this node),
  //    does not produce a graph output (node must produce fp16 output for the graph output),
  //    and is assigned to the CPU EP (we have fp32 implementations of all kernels so forcing to fp32 is safe)
  if ((no_cpu_kernel || (node.GetInputEdgesCount() > 0 &&
                         !node.ContainsSubgraph() &&
                         !graph.NodeProducesGraphOutput(node))) &&
      node.GetExecutionProviderType() == kCpuExecutionProvider) {
    //
    // Three tasks here:
    // 1. make sure all tensor(float16) inputs and first output coming from or
    //    going to nodes that will be cast to fp32
    // 2. check the current node is float16 node.
    // 3. check the current node has a float32 implementation
    // Only return true when all three are satisfied
    //
    const auto* schema = node.Op();
    if (!schema) {
      // no way to know whether it is safe to convert this to fp32, give up
      return false;
    }
 
    const TypeConstraintMap& type_schema = schema->typeConstraintMap();
    InlinedHashMap<std::string, MLDataType> type_constraint_map;
    type_constraint_map.reserve(type_schema.size());
 
    // For each formal parameters, there might be 0-n
    // actual inputs, this makes it very tricky to find out which
    // actual input should map to which formal parameter
 
    const auto& input_arg_counts = node.InputArgCount();
    const auto& input_defs = node.InputDefs();
    const auto& formal_inputs = schema->inputs();
    const size_t num_inputs = std::min(formal_inputs.size(), input_arg_counts.size());
 
    InlinedHashSet<int> fp16_args;
    int input_idx_start = 0;
    for (size_t formal_idx = 0;
         formal_idx < num_inputs;
         input_idx_start += input_arg_counts[formal_idx], formal_idx++) {
      const auto& type_str = formal_inputs[formal_idx].GetTypeStr();
      TypeConstraintMap::const_iterator it = type_schema.find(type_str);
      if (it == type_schema.end()) {
        // Don't care about parameter that does not have a type constraint.
        continue;
      }
 
      // type_str is like T, T1 or T2 ...
      for (int input_idx = 0; input_idx < input_arg_counts[formal_idx]; input_idx++) {
        const size_t idx = static_cast<size_t>(input_idx_start) + static_cast<size_t>(input_idx);
        ORT_ENFORCE(idx < input_defs.size());
        const NodeArg* input_def = input_defs[idx];
        if (!input_def || !input_def->Exists()) {
          continue;
        }
        if (IsMLFloat16Tensor(*input_def)) {
          fp16_args.emplace(static_cast<int>(idx));
          type_constraint_map[type_str] = DataTypeImpl::GetTensorType<float>();
          break;  // we don't have multiple tensors feeding into one input
        }
        type_constraint_map[type_str] = DataTypeImpl::TypeFromProto(*(input_def->TypeAsProto()));
        break;  // we don't have multiple tensors feeding into one input
      }
    }
 
    if (fp16_args.empty() && !no_cpu_kernel) {
      return false;  // no fp16 input
    }
 
    // check if all nodes providing our fp16 input need to be cast to fp32
    for (auto input_edge = node.InputEdgesBegin(), end = node.InputEdgesEnd(); input_edge != end; ++input_edge) {
      const int arg_idx = input_edge->GetDstArgIndex();
      if (fp16_args.find(arg_idx) != fp16_args.end()) {
        // if the node producing our fp16 input does not need its input cast to fp32 we should run in fp16
        if (!no_cpu_kernel && !NodeNeedsInputCastToFp32(input_edge->GetNode())) {
          return false;
        }
      }
    }
 
    // if we got here all nodes providing our fp16 input/s will be cast to fp32.
    // check if the same applies to the nodes consuming our fp16 output.
    fp16_args.clear();
    const auto& output_defs = node.OutputDefs();
    const auto& formal_outputs = schema->outputs();
    const size_t num_outputs = std::min(formal_outputs.size(), output_defs.size());
    for (size_t idx = 0; idx < num_outputs; idx++) {
      const auto& type_str = formal_outputs[idx].GetTypeStr();
      TypeConstraintMap::const_iterator it = type_schema.find(type_str);
      if (it == type_schema.end()) {
        // Don't care about parameter that does not have a type constraint.
        continue;
      }
 
      const NodeArg* output_def = output_defs[idx];
      if (!output_def || !output_def->Exists()) {
        continue;
      }
      if (IsMLFloat16Tensor(*output_def)) {
        fp16_args.emplace((int)idx);
        type_constraint_map[type_str] = DataTypeImpl::GetTensorType<float>();
      } else {
        type_constraint_map[type_str] = DataTypeImpl::TypeFromProto(*(output_def->TypeAsProto()));
      }
    }
 
    if (fp16_args.empty() && !no_cpu_kernel) {
      return false;  // no fp16 output
    }
 
    for (auto output_edge = node.OutputEdgesBegin(), end = node.OutputEdgesEnd(); output_edge != end; ++output_edge) {
      const int arg_idx = output_edge->GetSrcArgIndex();
      if (fp16_args.find(arg_idx) != fp16_args.end()) {
        // if the node consuming our fp16 output does not need its input cast to fp32 we should run in fp16
        if (!no_cpu_kernel && !NodeNeedsInputCastToFp32(output_edge->GetNode())) {
          return false;
        }
      }
    }
 
    // now all fp16 inputs and outputs would have a cast
    // make sure fp32 version of the kernel is available.
    const KernelCreateInfo* kernel_create_info{};
    const bool have_fp32_kernel =
        std::any_of(cpu_kernel_registries.cbegin(), cpu_kernel_registries.cend(),
                    [&](const gsl::not_null<const KernelRegistry*>& registry) {
                      const auto lookup_status = registry->TryFindKernel(
                          kCpuExecutionProvider, node.OpType(), node.Domain(),
                          node.SinceVersion(), type_constraint_map, logger, &kernel_create_info);
                      return lookup_status.IsOK() && kernel_create_info != nullptr;
                    });
    if (have_fp32_kernel) {
      return true;
    }
  }
 
  return false;
}
 
// These nodes have an fp16 CPU kernel and were therefore assigned to CPU EP by the partitioner;
// that assignment has already been recorded.  Clearing the EP is the mechanism that makes
// NeedInsertCast return true so they get wrapped with fp32 casts like any other unassigned node.
// Collect their indices so ApplyImpl can skip the on_partition_assignment_fn_ callback for them:
// the callback is only for nodes that are newly receiving a CPU EP fallback from this transformer,
// not for nodes whose partitioner assignment is already on record.
//
// nodes_forced_to_fp32 collects every node unassigned here, whether or not it has an fp16 kernel.
// ApplyImpl needs it to catch the ones NeedInsertCast will not fire on; see its use there.
static Status ForceSingleNodeCPUFloat16ToFloat32(onnxruntime::Graph& graph,
                                                 const KernelRegistryList& cpu_kernel_registries,
                                                 const logging::Logger& logger,
                                                 InlinedHashSet<NodeIndex>& nodes_with_recorded_cpu_assignment,
                                                 InlinedHashSet<NodeIndex>& nodes_forced_to_fp32) {
  for (auto& node : graph.Nodes()) {
    const bool no_cpu_kernel = IsFp16NodeOnCpuWithoutKernel(node, cpu_kernel_registries, logger);
    if (IsIsolatedFp16NodeOnCpu(node, graph, cpu_kernel_registries, logger, no_cpu_kernel)) {
      // unassign the node so that NeedInsertCast will return true for it, forcing it to fp32.
      if (!no_cpu_kernel) {
        // A node that has an fp16 kernel was assigned to the CPU EP by the partitioner, which
        // recorded that assignment, so ApplyImpl must not fire the callback for it again. One
        // without a kernel got its EP from a fusion that ran after partitioning, so nothing was
        // recorded and the callback does need to run -- hence it is left out of the set.
        nodes_with_recorded_cpu_assignment.insert(node.Index());
      }
      nodes_forced_to_fp32.insert(node.Index());
      node.SetExecutionProviderType("");
    } else if (no_cpu_kernel) {
      // The node has no fp16 CPU kernel, but IsIsolatedFp16NodeOnCpu could not confirm an fp32 one
      // to fall back to either, so it is left alone and will fail kernel lookup during session
      // initialization.
      LOGS(logger, WARNING) << "Node '" << node.Name() << "' (" << node.OpType()
                            << ") is assigned to the CPU EP and uses fp16, but has no fp16 kernel "
                               "and no fp32 kernel to fall back to.";
    }
  }
 
  return Status::OK();
}
 
enum TypeGroup {
  Unknown = -1,
  Bool = 0,
  Integer = 1,
  Unsigned = 2,
  Float = 3,
};
 
TypeGroup GetTypeGroup(DataType type) {
  if (*type == "tensor(bool)") {
    return Bool;
  }
 
  if (*type == "tensor(int16)" || *type == "tensor(int32)" || *type == "tensor(int64)" || *type == "tensor(int8)") {
    return Integer;
  }
 
  if (*type == "tensor(uint16)" || *type == "tensor(uint32)" || *type == "tensor(uint64)" || *type == "tensor(uint8)") {
    return Unsigned;
  }
 
  if (*type == "tensor(bfloat16)" || *type == "tensor(double)" || *type == "tensor(float)" || *type == "tensor(float16)") {
    return Float;
  }
 
  return Unknown;
}
 
int BitLength(DataType type) {
  if (*type == "tensor(bool)") {
    return 1;
  } else if (*type == "tensor(uint8)" || *type == "tensor(int8)") {
    return 8;
  } else if (*type == "tensor(int16)" || *type == "tensor(uint16)" || *type == "tensor(bfloat16)" || *type == "tensor(float16)") {
    return 16;
  } else if (*type == "tensor(int32)" || *type == "tensor(uint32)" || *type == "tensor(float)") {
    return 32;
  } else if (*type == "tensor(int64)" || *type == "tensor(uint64)" || *type == "tensor(double)") {
    return 64;
  } else {
    return -1;
  }
}
 
/** Transformer to remove duplicate Cast nodes. */
class RemoveDuplicateCastTransformer : public GraphTransformer {
 public:
  RemoveDuplicateCastTransformer() : GraphTransformer("RemoveDuplicateCastTransformer") {
  }
 
 private:
  static bool UnsafeCast(DataType src_type, DataType dst_type, const Node& node) {
    // This is not a complete cast optimisation pass, and is more conservative than it could be.
    // For instance, certain integral -> floating point casts could be optimized but
    // this is left to an explicit cast optimisation pass.
 
    // The comparison with "InsertedPrecisionFreeCast_" reflects cast nodes that are inserted by InsertCastTransformer.
    // Such casts should not be considered as loss of precision - the inserted upcasts (f16 -> f32) and
    // downcasts (f32 -> f16) are inserted to support kernels when on a CPU EP without F16 support.
    auto src_type_group = GetTypeGroup(src_type);
    auto dst_type_group = GetTypeGroup(dst_type);
    if (Unknown == src_type_group || Unknown == dst_type_group) {
      return true;
    }
 
    // Do not remove any signed -> unsigned cast.
    if ((src_type_group != Bool && src_type_group != Unsigned) && Unsigned == dst_type_group) {
      return true;
    }
 
    // Do not remove any floating point -> non floating point cast.
    if (Float == src_type_group && Float != dst_type_group) {
      return true;
    }
 
    auto src_bit_length = BitLength(src_type);
    auto dst_bit_length = BitLength(dst_type);
 
    // unsigned integer -> integer cast may overflow if the destination integer is smaller or equal to the source integer.
    if (Unsigned == src_type_group && Integer == dst_type_group) {
      return dst_bit_length <= src_bit_length;
    }
 
    // integral -> floating cast may overflow if integer cannot be encoded in the mantissa. This check could be more precise.
    if ((Integer == src_type_group || Unsigned == src_type_group) && Float == dst_type_group) {
      return dst_bit_length <= src_bit_length;
    }
 
    if ((*src_type == "tensor(float16)" && *dst_type == "tensor(bfloat16)") ||
        (*src_type == "tensor(bfloat16)" && *dst_type == "tensor(float16)")) {
      return true;
    }
 
    return src_bit_length > dst_bit_length && (node.Name().compare(0, 26, "InsertedPrecisionFreeCast_"));
  }
 
  Status ApplyImpl(Graph& graph, bool& modified, int graph_level, const logging::Logger& logger) const override {
    auto output_args = graph.GetOutputs();
    InlinedHashSet<const onnxruntime::NodeArg*> graph_outputs;
    graph_outputs.reserve(output_args.size());
    graph_outputs.insert(output_args.begin(), output_args.end());
    const auto graph_outputs_end = graph_outputs.end();
 
    for (auto& node : graph.Nodes()) {
      bool removed = false;
      if (node.OpType() == "Cast") {
        InlinedVector<std::reference_wrapper<Node>> nodes_to_remove;
        InlinedVector<std::reference_wrapper<Node>> cast_nodes_to_keep;
 
        // if cast's next node is also cast:
        //     - if the next cast's output type is equal to cast's input type, remove these two casts.
        //     - otherwise, remove the first cast.
        // Below are some exception cases for this optimization:
        //     - it's for non-numeric type casting.
        //     - if the casts are for (high precision -> low precision -> high precision),
        //       since there is actual loss of precision.
        //     - if the first cast loses precision and a downstream cast targets bool,
        //       since removing it changes zero/non-zero semantics (e.g., float->int truncation
        //       before a bool cast). See https://github.com/microsoft/onnxruntime/issues/28089
        // Other cases are OK for this optimization, including below two cases,
        // which are not actual loss of precision:
        //     - (low precision -> high precision -> low precision)
        //     - (high precision -> low precision -> lower precision) when not targeting bool
        // It's possible that there are more than one casts following the first cast,
        // the first cast can be removed only when:
        //     - not providing graph output, and
        //     - all consumer nodes are cast nodes, and
        //     - for each consumer cast node, it meets above condition for this optimization.
        auto src_type = node.InputDefs()[0]->Type();
        auto dst_type = node.OutputDefs()[0]->Type();
 
        bool loss_precision_cast = UnsafeCast(src_type, dst_type, node);
        size_t num_children = node.GetOutputEdgesCount();
 
        bool inconsistent_casts = false;
        for (auto it = node.OutputNodesBegin(); it != node.OutputNodesEnd(); ++it) {
          const Node& output_node(*it);
          if (output_node.OpType() == "Cast") {
            auto src_type1 = output_node.InputDefs()[0]->Type();
            auto dst_type1 = output_node.OutputDefs()[0]->Type();
            if (loss_precision_cast && UnsafeCast(dst_type1, src_type1, output_node)) {
              inconsistent_casts = true;
              break;
            }
 
            // Cannot remove node if it's output is also an output of the graph
            if (graph_outputs.find(output_node.OutputDefs()[0]) == graph_outputs_end &&
                src_type == dst_type1 && src_type1 == dst_type) {
              // get a mutable reference to the output node and save it
              nodes_to_remove.push_back(*graph.GetNode(output_node.Index()));
            } else {
              cast_nodes_to_keep.push_back(*graph.GetNode(output_node.Index()));
            }
          }
        }
 
        if (inconsistent_casts) {
          continue;
        }
 
        if (!nodes_to_remove.empty()) {
          if (node.GetInputEdgesCount() == 0) {
            // replacing with initializer or graph input so we just need the NodeArg for the input
            auto& input = *node.MutableInputDefs()[0];
 
            for (Node& node_to_remove : nodes_to_remove) {
              NodeIndex node_idx = node_to_remove.Index();
 
              // copy the edges so we can remove as we iterate them
              std::vector<Node::EdgeEnd> edges(node_to_remove.OutputEdgesBegin(), node_to_remove.OutputEdgesEnd());
 
              for (auto edge = edges.cbegin(), end = edges.cend(); edge != end; ++edge) {
                int dst_idx = edge->GetDstArgIndex();
                graph.RemoveEdge(node_idx, edge->GetNode().Index(), edge->GetSrcArgIndex(), dst_idx);
 
                // replace the input of the downstream nodes with the initializer
                Node& mutable_target = *graph.GetNode(edge->GetNode().Index());
                graph_utils::ReplaceNodeInput(mutable_target, dst_idx, input);
              }
 
              graph.RemoveNode(node_idx);
            }
          } else {
            // replace the output from the second Cast node with the input to 'node'
            const Node::EdgeEnd& input_edge = *node.InputEdgesBegin();
            Node& mutable_src_node = *graph.GetNode(input_edge.GetNode().Index());
            int replacement_idx = input_edge.GetSrcArgIndex();
 
            for (auto& n : nodes_to_remove) {
              Node& node_to_remove = n;
              // replace output index 0 (Cast only produces one output)
              graph_utils::ReplaceDownstreamNodeInput(graph, node_to_remove, 0, mutable_src_node, replacement_idx);
 
              graph.RemoveNode(node_to_remove.Index());
            }
          }
 
          modified = true;
        }
 
        // If all the child nodes are either removed or another Cast node and we're not providing graph output,
        // we can remove this node. Connect those remaining child Cast nodes to current Cast node's input.
        //
        // However, we must NOT do this if the first cast loses precision AND any kept child casts to bool.
        // Bool conversion (non-zero → true, zero → false) interacts badly with lossy intermediate casts
        // that can map non-zero values to zero, changing the semantics.
        // For example, Cast(float->int32) -> Cast(int32->bool) must not become Cast(float->bool)
        // because float->int32 truncates (e.g. -0.1 -> 0 -> false), whereas float->bool would give true.
        //
        // We also must NOT do this if any kept Cast child is on a different EP than the current node.
        // Fusing across EP boundaries can produce a node whose input type is not supported by its EP.
        // For example, Cast(int64->float, CPU) -> Cast(float->float16, WebGPU) would become
        // Cast(int64->float16, WebGPU), but WebGPU doesn't support int64 inputs.
        // See: https://github.com/microsoft/onnxruntime/issues/27291
        if (num_children > 0 && nodes_to_remove.size() + cast_nodes_to_keep.size() == num_children &&
            graph_outputs.find(node.OutputDefs()[0]) == graph_outputs_end) {
          // Check that all kept Cast children are on the same EP as the current node.
          // An empty EP means the node has not been assigned yet (e.g. pre-partitioning or in tests),
          // so we only flag a cross-EP conflict when both EPs are explicitly assigned and different.
          bool cross_ep = false;
          const auto& current_ep = node.GetExecutionProviderType();
          for (const auto& n : cast_nodes_to_keep) {
            const Node& kept_node = n;
            const auto& kept_ep = kept_node.GetExecutionProviderType();
            if (!current_ep.empty() && !kept_ep.empty() && kept_ep != current_ep) {
              cross_ep = true;
              break;
            }
          }
 
          if (!cross_ep) {
            // Check if any kept child Cast targets bool when the first cast is lossy.
            // Bool conversion tests for zero/non-zero, so any lossy intermediate cast
            // that maps non-zero values to zero (e.g. float truncation) changes the result.
            bool is_loss_cast_and_child_cast_to_bool = false;
            if (loss_precision_cast) {
              for (const auto& n : cast_nodes_to_keep) {
                const Node& kept_node = n;
                auto kept_dst_type = kept_node.OutputDefs()[0]->Type();
                if (kept_dst_type != nullptr && GetTypeGroup(kept_dst_type) == Bool) {
                  is_loss_cast_and_child_cast_to_bool = true;
                  break;
                }
              }
            }
 
            if (!is_loss_cast_and_child_cast_to_bool) {
              for (auto& n : cast_nodes_to_keep) {
                Node& cast_node_to_keep = n;
                graph.SetNodeArgType(*cast_node_to_keep.MutableInputDefs()[0], *node.InputDefs()[0]->TypeAsProto());
              }
 
              removed = graph_utils::RemoveNode(graph, node);
              modified = true;
            }
          }
        }
      }
 
      if (!removed) {
        ORT_RETURN_IF_ERROR(Recurse(node, modified, graph_level, logger));
      }
    }
 
    return Status::OK();
  }
};
 
Status InsertCastTransformer::ApplyImpl(onnxruntime::Graph& graph, bool& modified, int graph_level,
                                        const logging::Logger& logger) const {
  // This transformer implements a targeted fallback policy: any unassigned node with an fp16 input
  // is rewritten to consume fp32 (via inserted casts) and given a CPU EP assignment.
  // on_partition_assignment_fn_ is fired to record each such new CPU EP assignment.
  //
  // Exception: ForceSingleNodeCPUFloat16ToFloat32 may clear the EP of nodes that were already
  // assigned to CPU EP by the partitioner (they have an fp16 CPU kernel and got cast-wrapped to
  // avoid isolated fp16 islands).  Those nodes are tracked here so we can skip the callback —
  // their assignment was already recorded by the partitioner.
  InlinedHashSet<NodeIndex> nodes_with_recorded_cpu_assignment;
  InlinedHashSet<NodeIndex> nodes_forced_to_fp32;
  if (force_cpu_fp32_)
    ORT_RETURN_IF_ERROR(ForceSingleNodeCPUFloat16ToFloat32(graph, cpu_kernel_registries_, logger,
                                                           nodes_with_recorded_cpu_assignment,
                                                           nodes_forced_to_fp32));
 
  GraphViewer graph_viewer(graph);
  auto& order = graph_viewer.GetNodesInTopologicalOrder();
  TypeProto float_16_tensor_proto;
  TypeProto float_tensor_proto;
  float_16_tensor_proto.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT16);
  float_tensor_proto.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT);
  std::map<onnxruntime::NodeArg*, onnxruntime::NodeArg*> input_def_updates;
 
  for (onnxruntime::NodeIndex i : order) {
    auto node = graph.GetNode(i);
    if (!node)
      return Status(ONNXRUNTIME, INVALID_ARGUMENT);
 
    auto& inputs = node->MutableInputDefs();
    std::map<const onnxruntime::NodeArg*, onnxruntime::NodeArg*> replacement_defs;
    bool casted = false;
    for (auto input : inputs) {
      if (NeedInsertCast(node, input)) {
        auto src_arg = input;
        if (input_def_updates.count(src_arg)) {
          replacement_defs[src_arg] = input_def_updates[src_arg];
        } else {
          // insert cast op to cast input
          auto dst_arg = AddCastNode(graph,
                                     src_arg,
                                     &float_tensor_proto,
                                     false,
                                     static_cast<int64_t>(TensorProto_DataType_FLOAT),
                                     // right now we only cast for cpu cases.
                                     onnxruntime::kCpuExecutionProvider);
          replacement_defs[src_arg] = dst_arg;
          input_def_updates[src_arg] = dst_arg;
        }
        casted = true;
      }
    }
 
    // ForceSingleNodeCPUFloat16ToFloat32 may have unassigned a node whose only fp16 value is an
    // output -- a generator op such as RandomNormal(dtype=float16) has no inputs at all -- so
    // NeedInsertCast never fires above and `casted` stays false. Such a node still needs the CPU EP
    // (re-)assigned and its dtype/output fixed up below, exactly like one that got an input cast.
    const bool force_output_only_conversion =
        !casted && nodes_forced_to_fp32.find(node->Index()) != nodes_forced_to_fp32.end();
 
    if (casted || force_output_only_conversion) {
      // Set current node to run on the CPU execution provider
      // Keep in mind that the EP will be empty because NeedInsertCast() already insures that
      node->SetExecutionProviderType(kCpuExecutionProvider);
 
      // Record the new CPU EP assignment via the partition assignment callback if provided.
      // Skip nodes in nodes_with_recorded_cpu_assignment: their CPU EP assignment was made by the partitioner
      // and is already on record; the callback must not fire again for them.
      if (on_partition_assignment_fn_ && nodes_with_recorded_cpu_assignment.find(node->Index()) == nodes_with_recorded_cpu_assignment.end()) {
        auto sub_graph = std::make_unique<IndexedSubGraph>();
        sub_graph->nodes = {node->Index()};
        ComputeCapability capability(std::move(sub_graph));
        on_partition_assignment_fn_(graph, capability, kCpuExecutionProvider);
      }
 
      // Some ONNX operators have an attribute `dtype` which define the output type for these operators
      // (mostly Generator ops like RandomNormal, RandomNormalLike, EyeLike, etc.).
      // Update that so that `dtype` is now Float. Otherwise there could be a mis-match between the actual
      // type of the NodeArg and the ONNX inferred type of the NodeArg and Graph Resolve() will complain.
      auto& attributes = node->GetMutableAttributes();
      auto dtype_attribute = attributes.find("dtype");
 
      if (dtype_attribute != attributes.end()) {
        // Simple sanity check
        ORT_ENFORCE(dtype_attribute->second.has_i(),
                    "InsertCastTransformer works on the assumption that `dtype` attribute holds an integer.");
 
        // Modify the dtype attribute (which defines the output type) to FLOAT if it is FLOAT16.
        if (dtype_attribute->second.i() == TensorProto_DataType_FLOAT16) {
          dtype_attribute->second.set_i(TensorProto_DataType_FLOAT);
        }
      }
 
      if (node->OpType() == "Cast" && node->Domain() == kOnnxDomain) {
        auto to_attribute = attributes.find("to");
        if (to_attribute != attributes.end() &&
            to_attribute->second.i() == TensorProto_DataType_FLOAT16) {
          to_attribute->second.set_i(TensorProto_DataType_FLOAT);
        }
      }
 
      auto& outputs = node->MutableOutputDefs();
      for (auto output : outputs) {
        // TODO 1: Check if the kernel available
        // TODO 2: There is an inherent assumption that if we cast a cpu op's input from float16 to float
        // then this cpu op's output will be float (if it was inferred to be float16 previously).
        // Not sure if this is always true. Handle any corner case if it does exist.
 
        if (IsMLFloat16Tensor(*output)) {
          // insert cast op to cast output back to float16
          auto dst_arg = output;
          auto src_arg = AddCastNode(graph,
                                     dst_arg,
                                     &float_tensor_proto,
                                     true,
                                     static_cast<int64_t>(TensorProto_DataType_FLOAT16),
                                     onnxruntime::kCpuExecutionProvider);
          replacement_defs[dst_arg] = src_arg;
        }
      }
 
      node->ReplaceDefs(replacement_defs);
      modified = true;
    }
 
    ORT_RETURN_IF_ERROR(Recurse(*node, modified, graph_level, logger));
  }
 
  auto status = Status::OK();
 
  // if this is the main graph we've recursed into all the subgraphs and added Cast nodes.
  // run the duplicate remover now, which will call Graph::Resolve from Apply(...) and handle the main and subgraphs.
  if (graph_level == 0) {
    if (modified) {
      ORT_RETURN_IF_ERROR(graph.Resolve());
    }
 
    // if we had multiple nodes in a row that were converted to fp32 we will have casts around every node.
    // Casts in between converted nodes cancel each other out and can be removed.
    // e.g.
    //      -> NodeA(fp16) -> NodeB(fp16) ->
    // After converting both to fp32
    //      -> CastToFp32 -> NodeA(fp32) -> CastToFp16 -> CastToFp32 -> NodeB(fp32) -> CastToFp16
    // After running duplicate cast removal
    //      -> CastToFp32 -> NodeA(fp32) -> NodeB(fp32) -> CastToFp16
    //
    RemoveDuplicateCastTransformer remover;
    status = remover.Apply(graph, modified, logger);
  }
 
  return status;
}
}  // namespace onnxruntime