diff --git a/python/python/lancedb/functions.py b/python/python/lancedb/functions.py index b2e4cb0eb..d1857de55 100644 --- a/python/python/lancedb/functions.py +++ b/python/python/lancedb/functions.py @@ -93,7 +93,7 @@ def _normalize_gpu_request( if gpu_class is not None: gpu_class = _validate_gpu_class_name(gpu_class) if gpu_count is None: - gpu_count = 1 + return True, gpu_class, None if isinstance(gpu_count, bool) or not isinstance(gpu_count, int): raise ValueError("gpu_count must be a positive integer") if not 1 <= gpu_count <= 2**32 - 1: @@ -4046,10 +4046,11 @@ def function( any eligible configured GPU class. Use ``"default"`` to select the deployment's default pool explicitly. gpu_count : int, optional - Default GPUs allocated together to each Function worker. Requires ``gpu=True`` - and defaults to one. Column/MV creation and refresh can override it. - Must be a positive integer. Refresh - ``total_gpu_count`` limits the total GPUs across the worker fleet. + GPUs allocated together to each Function worker. Requires ``gpu=True`` + and must be a positive integer. When omitted, the registration request + leaves the count unset and the server defaults to one. Column/MV + creation and refresh can override it. Refresh ``total_gpu_count`` limits + the total GPUs across the worker fleet. total_cpu_millis : int, optional Default total CPU budget for an execution, in millicores (1000 = one core). A column/MV declaration or refresh may override this value. diff --git a/python/python/tests/test_function_creation.py b/python/python/tests/test_function_creation.py index ae5a2bf8b..94109bf13 100644 --- a/python/python/tests/test_function_creation.py +++ b/python/python/tests/test_function_creation.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright The LanceDB Authors +import hashlib import json from pathlib import Path @@ -73,6 +74,73 @@ async def test_function_exist_ok_forwards_sql_and_keeps_stored_version(entry, ex assert json.loads(version.to_canonical_json()) == stored +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("gpu_options", "expected_gpu"), + [ + pytest.param({}, {}, id="cpu"), + pytest.param({"gpu": True}, {"gpu": True}, id="implicit"), + pytest.param( + {"gpu": True, "gpu_count": 1}, {"gpu": True, "gpus_per_worker": 1}, id="one" + ), + pytest.param( + {"gpu": True, "gpu_count": 2}, {"gpu": True, "gpus_per_worker": 2}, id="two" + ), + ], +) +async def test_function_registration_gpu_count_and_runtime_digest( + gpu_options, expected_gpu +): + flight = pytest.importorskip("pyarrow.flight") + stored = _stored_function_version() + + @lancedb.function( + name=stored["name"], + python_version="3.12", + input_schema=pa.schema([pa.field("value", pa.int64())]), + output_schema=pa.int64(), + **gpu_options, + ) + def identity(value: pa.Array) -> pa.Array: + return value + + class FunctionServer(flight.FlightServerBase): + def __init__(self): + super().__init__("grpc://127.0.0.1:0") + self.requests = [] + + def do_get(self, context, ticket): + self.requests.append(ticket.ticket) + return flight.RecordBatchStream( + pa.table({"definition": [json.dumps(stored)]}) + ) + + with FunctionServer() as server: + db = await lancedb.connect_async( + "db://test", + api_key="test-key", + host_override="http://localhost:10024", + sql_host_override=f"grpc://127.0.0.1:{server.port}", + ) + await db.create_function_async(identity) + (request,) = server.requests + + assert b"CREATE FUNCTION" in request + _, marker, body = request.partition(b" FROM '{") + assert marker + submitted, _ = json.JSONDecoder().raw_decode("{" + body.decode()) + expected_runtime = { + "kind": "python_v2" if expected_gpu else "python", + "python_version": "3.12", + "environment": {"kind": "pip"}, + **expected_gpu, + } + assert submitted["runtime"] == expected_runtime + canonical = json.dumps(expected_runtime, sort_keys=True, separators=(",", ":")) + expected_digest = hashlib.sha256(canonical.encode()).hexdigest() + assert identity.registration_request.runtime.digest == f"sha256:{expected_digest}" + + @pytest.mark.asyncio async def test_blob_writer_paths_round_trip_through_function_registration(): flight = pytest.importorskip("pyarrow.flight")