-
-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone: Added support for "tensorflow.function" JIT
* This adds support to preserve the source code of decorated functions and provide it at runtime to tensorflow so it can do its tracing. * The code generation now has a way of providing module level init codes. * This mechanism should be possible to generalize to other JIT making modules as well. * Without this, some codes using tensorflow.function could totally miss out on the specialization it does, that compilation with Nuitka does not currently replace.
- Loading branch information
Showing
25 changed files
with
1,121 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2024, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file | ||
|
||
#ifndef __NUITKA_JIT_SOURCES_H__ | ||
#define __NUITKA_JIT_SOURCES_H__ | ||
|
||
// Helpers for making source available at run-time for JIT systems | ||
// outside of Nuitka that want it. | ||
|
||
extern void SET_UNCOMPILED_FUNCTION_SOURCE_DICT(PyObject *name, PyObject *source); | ||
|
||
#endif | ||
// Part of "Nuitka", an optimizing Python compiler that is compatible and | ||
// integrates with CPython, but also works on its own. | ||
// | ||
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2024, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file | ||
|
||
// This file is included from another C file, help IDEs to still parse it on | ||
// its own. | ||
#ifdef __IDE_ONLY__ | ||
#include "nuitka/prelude.h" | ||
#endif | ||
|
||
#ifdef _NUITKA_STANDALONE | ||
|
||
static char const *uncompiled_sources_dict_attribute_name = "_uncompiled_function_sources_dict"; | ||
|
||
void SET_UNCOMPILED_FUNCTION_SOURCE_DICT(PyObject *name, PyObject *source) { | ||
PyObject *uncompiled_function_sources_dict = | ||
PyObject_GetAttrString((PyObject *)builtin_module, uncompiled_sources_dict_attribute_name); | ||
|
||
if (uncompiled_function_sources_dict == NULL) { | ||
PyThreadState *tstate = PyThreadState_GET(); | ||
|
||
DROP_ERROR_OCCURRED(tstate); | ||
|
||
uncompiled_function_sources_dict = MAKE_DICT_EMPTY(); | ||
|
||
PyObject_SetAttrString((PyObject *)builtin_module, uncompiled_sources_dict_attribute_name, | ||
uncompiled_function_sources_dict); | ||
} | ||
|
||
bool res = DICT_SET_ITEM(uncompiled_function_sources_dict, name, source); | ||
assert(res == false); | ||
} | ||
|
||
#endif | ||
// Part of "Nuitka", an optimizing Python compiler that is compatible and | ||
// integrates with CPython, but also works on its own. | ||
// | ||
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2024, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file | ||
|
||
|
||
""" Code generation for tensorflow module specific stuff. """ | ||
|
||
from nuitka.Options import isStandaloneMode | ||
|
||
from .BuiltinCodes import getBuiltinCallViaSpecCode | ||
from .ImportCodes import getImportModuleNameHardCode | ||
|
||
|
||
def generateTensorflowFunctionCallCode(to_name, expression, emit, context): | ||
"""This is for tensorflow.function calls.""" | ||
|
||
# TODO: Have global cached forms of hard attribute lookup results too. | ||
tensorflow_function_name = context.allocateTempName( | ||
"tensorflow_function", unique=True | ||
) | ||
|
||
getImportModuleNameHardCode( | ||
to_name=tensorflow_function_name, | ||
module_name="tensorflow", | ||
import_name="function", | ||
needs_check=False, | ||
emit=emit, | ||
context=context, | ||
) | ||
|
||
# Include source code of "tensorflow.function" decorated functions. | ||
if expression.subnode_func is not None and isStandaloneMode(): | ||
func_value = expression.subnode_func | ||
|
||
if func_value.isExpressionFunctionCreation(): | ||
function_ref = func_value.subnode_function_ref | ||
|
||
function_super_qualified_name = function_ref.getFunctionSuperQualifiedName() | ||
function_source_code = function_ref.getFunctionSourceCode() | ||
|
||
context.addModuleInitCode( | ||
"""\ | ||
SET_UNCOMPILED_FUNCTION_SOURCE_DICT(%s, %s); | ||
""" | ||
% ( | ||
context.getConstantCode(function_super_qualified_name), | ||
context.getConstantCode(function_source_code), | ||
) | ||
) | ||
|
||
getBuiltinCallViaSpecCode( | ||
spec=expression.spec, | ||
called_name=tensorflow_function_name, | ||
to_name=to_name, | ||
expression=expression, | ||
emit=emit, | ||
context=context, | ||
) | ||
|
||
|
||
# Part of "Nuitka", an optimizing Python compiler that is compatible and | ||
# integrates with CPython, but also works on its own. | ||
# | ||
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.