pytorch/torch/csrc/jit/ir
Weinan Liu fa4cb91846 add support for ir scalar literal parsing for inf/-inf/True/False (#163924)
Currently the ir parser doesn't support parse ir like
```
graph():
  %12 : float = prim::Constant[value=-inf]()
  %13 : float = prim::Constant[value=inf]()
  %14 : bool = prim::Constant[value=True]()
  %15 : bool = prim::Constant[value=False]()
  return (%12)
```

So the python script below will throw error.

```
#!/bin/env python
import torch

def test():
    return [True, False]
f = torch.jit.script(test)
torch._C._jit_pass_constant_propagation(f.graph)
ts_str = f.graph.__repr__()
print(ts_str)
ts = torch.parse_ir(ts_str)
func = torch._C._create_function_from_graph("forward", ts)
ret = func()
assert ret == [True, False]

def test():
    return [float("inf"), float("-inf")]
f = torch.jit.script(test)
torch._C._jit_pass_constant_propagation(f.graph)
ts_str = f.graph.__repr__()
print(ts_str)
ts = torch.parse_ir(ts_str)
func = torch._C._create_function_from_graph("forward", ts)
ret = func()
assert ret == [float("inf"), float("-inf")]
```

I add "inf" and bool cases for IRParser::parseScalarLiteral in irparser.cpp.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/163924
Approved by: https://github.com/ezyang
2025-10-27 05:10:21 +00:00
..
alias_analysis.cpp
alias_analysis.h
attributes.cpp
attributes.h Turn some const strings into constexpr in C++ code (#165203) 2025-10-13 20:25:20 +00:00
constants.cpp
constants.h
graph_node_list.h
graph_utils.cpp
graph_utils.h
ir_views.h
ir.cpp [ROCm] Fix test_cuda_synchronize failure on ROCm (#164735) 2025-10-07 01:14:24 +00:00
ir.h [ROCm] Fix test_cuda_synchronize failure on ROCm (#164735) 2025-10-07 01:14:24 +00:00
irparser.cpp add support for ir scalar literal parsing for inf/-inf/True/False (#163924) 2025-10-27 05:10:21 +00:00
irparser.h
named_value.h
node_hashing.cpp
node_hashing.h
scope.cpp
scope.h [2/N] Mark unused parameters in C++ code (#165121) 2025-10-15 03:04:39 +00:00
subgraph_matcher.cpp
subgraph_matcher.h
type_hashing.cpp
type_hashing.h