MCPcopy Create free account
hub / github.com/MrNeRF/LichtFeld-Studio / runPythonTensorSnippet

Function runPythonTensorSnippet

tests/test_python_integration.cpp:312–356  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

310 }
311
312 PythonTensorResult runPythonTensorSnippet(const std::string& script) {
313 const lfs::python::GilAcquire gil;
314
315 PyObject* globals = PyDict_New();
316 PyObject* locals = PyDict_New();
317 if (!globals || !locals) {
318 Py_XDECREF(globals);
319 Py_XDECREF(locals);
320 throw std::runtime_error("Failed to allocate Python dictionaries");
321 }
322
323 PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
324
325 PyObject* exec_result = PyRun_String(script.c_str(), Py_file_input, globals, locals);
326 if (!exec_result) {
327 const auto error = consumePythonError();
328 Py_DECREF(globals);
329 Py_DECREF(locals);
330 throw std::runtime_error(error);
331 }
332 Py_DECREF(exec_result);
333
334 auto* const shape_obj = PyDict_GetItemString(locals, "result_shape");
335 auto* const values_obj = PyDict_GetItemString(locals, "result_values");
336 if (!shape_obj || !PyTuple_Check(shape_obj) || !values_obj || !PyList_Check(values_obj)) {
337 Py_DECREF(globals);
338 Py_DECREF(locals);
339 throw std::runtime_error("Python snippet did not populate result_shape/result_values");
340 }
341
342 PythonTensorResult result;
343 result.shape.reserve(static_cast<size_t>(PyTuple_Size(shape_obj)));
344 for (Py_ssize_t i = 0; i < PyTuple_Size(shape_obj); ++i) {
345 result.shape.push_back(PyLong_AsLongLong(PyTuple_GetItem(shape_obj, i)));
346 }
347
348 result.values.reserve(static_cast<size_t>(PyList_Size(values_obj)));
349 for (Py_ssize_t i = 0; i < PyList_Size(values_obj); ++i) {
350 result.values.push_back(static_cast<float>(PyFloat_AsDouble(PyList_GetItem(values_obj, i))));
351 }
352
353 Py_DECREF(globals);
354 Py_DECREF(locals);
355 return result;
356 }
357
358 std::vector<long long> runPythonHookContextSnippet(const std::string& registration_script,
359 const lfs::training::HookContext& snapshot_ctx,

Callers 1

TEST_FFunction · 0.85

Calls 4

consumePythonErrorFunction · 0.85
c_strMethod · 0.80
reserveMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected