2024-06-30 07:33:39 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2017-06-02 19:02:02 +00:00
|
|
|
import re
|
2024-12-03 07:35:18 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import Mapping, Sequence
|
2017-06-02 19:02:02 +00:00
|
|
|
|
2024-06-22 07:33:49 +00:00
|
|
|
|
2017-06-10 23:56:24 +00:00
|
|
|
# match $identifier or ${identifier} and replace with value in env
|
2017-06-03 03:08:47 +00:00
|
|
|
# If this identifier is at the beginning of whitespace on a line
|
|
|
|
|
# and its value is a list then it is treated as
|
2020-08-06 07:03:49 +00:00
|
|
|
# block substitution by indenting to that depth and putting each element
|
2017-06-03 03:08:47 +00:00
|
|
|
# of the list on its own line
|
|
|
|
|
# if the identifier is on a line starting with non-whitespace and a list
|
|
|
|
|
# then it is comma separated ${,foo} will insert a comma before the list
|
|
|
|
|
# if this list is not empty and ${foo,} will insert one after.
|
2017-06-02 19:02:02 +00:00
|
|
|
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2020-08-06 07:03:49 +00:00
|
|
|
class CodeTemplate:
|
2022-06-07 07:02:55 +00:00
|
|
|
substitution_str = r"(^[^\n\S]*)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})"
|
2020-08-06 07:03:49 +00:00
|
|
|
substitution = re.compile(substitution_str, re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
pattern: str
|
|
|
|
|
filename: str
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2017-06-10 23:56:24 +00:00
|
|
|
@staticmethod
|
2024-06-30 07:33:39 +00:00
|
|
|
def from_file(filename: str) -> CodeTemplate:
|
2023-07-19 07:33:53 +00:00
|
|
|
with open(filename) as f:
|
2019-02-28 17:37:48 +00:00
|
|
|
return CodeTemplate(f.read(), filename)
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2020-08-06 07:03:49 +00:00
|
|
|
def __init__(self, pattern: str, filename: str = "") -> None:
|
2017-06-02 19:02:02 +00:00
|
|
|
self.pattern = pattern
|
2019-02-28 17:37:48 +00:00
|
|
|
self.filename = filename
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2022-04-20 07:02:39 +00:00
|
|
|
def substitute(
|
2024-06-30 07:33:39 +00:00
|
|
|
self, env: Mapping[str, object] | None = None, **kwargs: object
|
2022-04-20 07:02:39 +00:00
|
|
|
) -> str:
|
2019-03-21 16:06:30 +00:00
|
|
|
if env is None:
|
|
|
|
|
env = {}
|
|
|
|
|
|
2020-08-06 07:03:49 +00:00
|
|
|
def lookup(v: str) -> object:
|
|
|
|
|
assert env is not None
|
2017-06-09 20:53:27 +00:00
|
|
|
return kwargs[v] if v in kwargs else env[v]
|
|
|
|
|
|
2020-08-06 07:03:49 +00:00
|
|
|
def indent_lines(indent: str, v: Sequence[object]) -> str:
|
2022-04-20 07:02:39 +00:00
|
|
|
return "".join(
|
|
|
|
|
[indent + l + "\n" for e in v for l in str(e).splitlines()]
|
|
|
|
|
).rstrip()
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2024-06-30 07:33:39 +00:00
|
|
|
def replace(match: re.Match[str]) -> str:
|
2017-06-02 19:02:02 +00:00
|
|
|
indent = match.group(1)
|
|
|
|
|
key = match.group(2)
|
2022-04-20 07:02:39 +00:00
|
|
|
comma_before = ""
|
|
|
|
|
comma_after = ""
|
2017-06-02 19:02:02 +00:00
|
|
|
if key[0] == "{":
|
|
|
|
|
key = key[1:-1]
|
2017-06-02 23:39:23 +00:00
|
|
|
if key[0] == ",":
|
2022-04-20 07:02:39 +00:00
|
|
|
comma_before = ", "
|
2017-06-02 23:39:23 +00:00
|
|
|
key = key[1:]
|
2022-04-20 07:02:39 +00:00
|
|
|
if key[-1] == ",":
|
|
|
|
|
comma_after = ", "
|
2017-06-02 23:39:23 +00:00
|
|
|
key = key[:-1]
|
2017-06-02 19:02:02 +00:00
|
|
|
v = lookup(key)
|
Switch interpreter to use IValue's primitive int/floats (#9718)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9718
This patch switches the interpreter to use IValue's primitive numbers rather than tensors for computing on integers and floats. In addition to preparing the interpreter for first-class support of other types, this cleans up the handling of primitive numbers, making it possible to just use the normal operator overloading dispatch to find the right implementation for numbers. As a result of this change, a lot of other functionality needed to be updated since it was the first time we use non-tensors in a lot of places in the code base.
Notes:
* Fixes code_template.py so that multi-line strings are indented correctly when used on a standalone line
* Cast operators (`int(x)`) now are functional. Some tests have addition conversions to integers because
we no longer allow implicit tensor -> integer conversions following the same convention as in python
* prim::ListConstruct/createList has been added to the interpreter for creating lists and this has
replaced aten::stack for integers lists
* gen_jit_dispatch.py has been refactored so that non-tensor types use operators on IValues to extract
the primitives
* IValue gains a .to<T> method that is the equivalent of tensor_as but for IValue instead of at::Tensor
* `constant_as<T>` is switched over to using IValues's `.to<T>` method, to make conversion from constant->IValue->C++ type
more consistent. This functionality combined with `toIValue(Value*)` replaces the `tensor_as` and `as_tensor` family of functions.
* conditional expressions (if, loop) and operators related to them are now computed on integers rather than tensors
* IValue gains constructors for constructing from at::Scalar and converting to it. However, IValue itself will always store
the scalars as a double or int64.
* To align with python 3 syntax, TK_INT, TK_FLOAT, and TK_BOOL have been removed from the parser, and int/float/bool are just treated as special identifiers in the compiler,
along with print. These are represented as special sugared values with a `call` method implemented. For int/float/bool this implements casting behavior.
* Dropped shared_from_this from Type/Module. They were not needed and they making debugging harder because they internally throw/catch exceptions.
* Shape propagation has been updated to support running nodes that include floating point primitive types, this required some refactoring of internal functions.
* TensorToNum and NumToTensor have actual implementations as operators now
* regster_prim_ops now contains implementations of math operators for float/int primitive types, and for mixed (prim <+> tensor) versions. This removes the need for special handling in compiler.cpp
* Primitive math is now entirely handled by letting the compiler choose the right overloads. This removes tons of special casing in the compiler.
* incorporates eellison's change to allow casting from return values. Due to the addition of primitive support, the code need slight modifications, so I just pre-merged it here.
* stack.h gains generic vararg versions of push/pop that know how to convert to/from C++ types:
```
at::Tensor a;
at::Scalar b;
pop(stack, a, b);
at::Tensor c = a + b;
push(stack, c);
```
apaszke
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9584
Reviewed By: apaszke
Differential Revision: D8910546
Pulled By: zdevito
fbshipit-source-id: 0f3e60d4d22217f196a8f606549430e43b7e7e30
2018-07-23 20:58:32 +00:00
|
|
|
if indent is not None:
|
|
|
|
|
if not isinstance(v, list):
|
|
|
|
|
v = [v]
|
2017-06-09 20:53:27 +00:00
|
|
|
return indent_lines(indent, v)
|
|
|
|
|
elif isinstance(v, list):
|
2022-04-20 07:02:39 +00:00
|
|
|
middle = ", ".join([str(x) for x in v])
|
2017-06-02 23:39:23 +00:00
|
|
|
if len(v) == 0:
|
|
|
|
|
return middle
|
2017-06-09 20:53:27 +00:00
|
|
|
return comma_before + middle + comma_after
|
2017-06-02 19:02:02 +00:00
|
|
|
else:
|
Switch interpreter to use IValue's primitive int/floats (#9718)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9718
This patch switches the interpreter to use IValue's primitive numbers rather than tensors for computing on integers and floats. In addition to preparing the interpreter for first-class support of other types, this cleans up the handling of primitive numbers, making it possible to just use the normal operator overloading dispatch to find the right implementation for numbers. As a result of this change, a lot of other functionality needed to be updated since it was the first time we use non-tensors in a lot of places in the code base.
Notes:
* Fixes code_template.py so that multi-line strings are indented correctly when used on a standalone line
* Cast operators (`int(x)`) now are functional. Some tests have addition conversions to integers because
we no longer allow implicit tensor -> integer conversions following the same convention as in python
* prim::ListConstruct/createList has been added to the interpreter for creating lists and this has
replaced aten::stack for integers lists
* gen_jit_dispatch.py has been refactored so that non-tensor types use operators on IValues to extract
the primitives
* IValue gains a .to<T> method that is the equivalent of tensor_as but for IValue instead of at::Tensor
* `constant_as<T>` is switched over to using IValues's `.to<T>` method, to make conversion from constant->IValue->C++ type
more consistent. This functionality combined with `toIValue(Value*)` replaces the `tensor_as` and `as_tensor` family of functions.
* conditional expressions (if, loop) and operators related to them are now computed on integers rather than tensors
* IValue gains constructors for constructing from at::Scalar and converting to it. However, IValue itself will always store
the scalars as a double or int64.
* To align with python 3 syntax, TK_INT, TK_FLOAT, and TK_BOOL have been removed from the parser, and int/float/bool are just treated as special identifiers in the compiler,
along with print. These are represented as special sugared values with a `call` method implemented. For int/float/bool this implements casting behavior.
* Dropped shared_from_this from Type/Module. They were not needed and they making debugging harder because they internally throw/catch exceptions.
* Shape propagation has been updated to support running nodes that include floating point primitive types, this required some refactoring of internal functions.
* TensorToNum and NumToTensor have actual implementations as operators now
* regster_prim_ops now contains implementations of math operators for float/int primitive types, and for mixed (prim <+> tensor) versions. This removes the need for special handling in compiler.cpp
* Primitive math is now entirely handled by letting the compiler choose the right overloads. This removes tons of special casing in the compiler.
* incorporates eellison's change to allow casting from return values. Due to the addition of primitive support, the code need slight modifications, so I just pre-merged it here.
* stack.h gains generic vararg versions of push/pop that know how to convert to/from C++ types:
```
at::Tensor a;
at::Scalar b;
pop(stack, a, b);
at::Tensor c = a + b;
push(stack, c);
```
apaszke
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9584
Reviewed By: apaszke
Differential Revision: D8910546
Pulled By: zdevito
fbshipit-source-id: 0f3e60d4d22217f196a8f606549430e43b7e7e30
2018-07-23 20:58:32 +00:00
|
|
|
return str(v)
|
2022-04-20 07:02:39 +00:00
|
|
|
|
2020-08-06 07:03:49 +00:00
|
|
|
return self.substitution.sub(replace, self.pattern)
|
2017-06-09 20:53:27 +00:00
|
|
|
|
2017-06-02 19:02:02 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-04-20 07:02:39 +00:00
|
|
|
c = CodeTemplate(
|
|
|
|
|
"""\
|
2017-06-02 19:02:02 +00:00
|
|
|
int foo($args) {
|
|
|
|
|
|
|
|
|
|
$bar
|
|
|
|
|
$bar
|
|
|
|
|
$a+$b
|
|
|
|
|
}
|
2017-06-02 23:39:23 +00:00
|
|
|
int commatest(int a${,stuff})
|
|
|
|
|
int notest(int a${,empty,})
|
2022-04-20 07:02:39 +00:00
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
print(
|
|
|
|
|
c.substitute(
|
|
|
|
|
args=["hi", 8],
|
|
|
|
|
bar=["what", 7],
|
|
|
|
|
a=3,
|
|
|
|
|
b=4,
|
|
|
|
|
stuff=["things...", "others"],
|
|
|
|
|
empty=[],
|
|
|
|
|
)
|
|
|
|
|
)
|