pixeltable 0.3.0__py3-none-any.whl → 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pixeltable might be problematic. Click here for more details.
- pixeltable/__version__.py +2 -2
- pixeltable/catalog/insertable_table.py +3 -3
- pixeltable/catalog/table.py +2 -2
- pixeltable/catalog/table_version.py +3 -2
- pixeltable/catalog/view.py +1 -1
- pixeltable/dataframe.py +52 -27
- pixeltable/env.py +109 -4
- pixeltable/exec/__init__.py +1 -1
- pixeltable/exec/aggregation_node.py +3 -3
- pixeltable/exec/cache_prefetch_node.py +13 -7
- pixeltable/exec/component_iteration_node.py +3 -9
- pixeltable/exec/data_row_batch.py +17 -5
- pixeltable/exec/exec_node.py +32 -12
- pixeltable/exec/expr_eval/__init__.py +1 -0
- pixeltable/exec/expr_eval/evaluators.py +240 -0
- pixeltable/exec/expr_eval/expr_eval_node.py +408 -0
- pixeltable/exec/expr_eval/globals.py +113 -0
- pixeltable/exec/expr_eval/row_buffer.py +76 -0
- pixeltable/exec/expr_eval/schedulers.py +240 -0
- pixeltable/exec/in_memory_data_node.py +2 -2
- pixeltable/exec/row_update_node.py +14 -14
- pixeltable/exec/sql_node.py +2 -2
- pixeltable/exprs/column_ref.py +5 -1
- pixeltable/exprs/data_row.py +50 -40
- pixeltable/exprs/expr.py +57 -12
- pixeltable/exprs/function_call.py +54 -19
- pixeltable/exprs/inline_expr.py +12 -21
- pixeltable/exprs/literal.py +25 -8
- pixeltable/exprs/row_builder.py +25 -2
- pixeltable/func/aggregate_function.py +4 -0
- pixeltable/func/callable_function.py +54 -4
- pixeltable/func/expr_template_function.py +5 -1
- pixeltable/func/function.py +48 -7
- pixeltable/func/query_template_function.py +16 -7
- pixeltable/func/udf.py +7 -1
- pixeltable/functions/__init__.py +1 -1
- pixeltable/functions/anthropic.py +97 -21
- pixeltable/functions/gemini.py +2 -6
- pixeltable/functions/openai.py +219 -28
- pixeltable/globals.py +2 -3
- pixeltable/io/hf_datasets.py +1 -1
- pixeltable/io/label_studio.py +5 -5
- pixeltable/io/parquet.py +1 -1
- pixeltable/metadata/__init__.py +2 -1
- pixeltable/plan.py +24 -9
- pixeltable/store.py +6 -0
- pixeltable/type_system.py +73 -36
- pixeltable/utils/arrow.py +3 -8
- pixeltable/utils/console_output.py +41 -0
- pixeltable/utils/filecache.py +1 -1
- {pixeltable-0.3.0.dist-info → pixeltable-0.3.2.dist-info}/METADATA +4 -1
- {pixeltable-0.3.0.dist-info → pixeltable-0.3.2.dist-info}/RECORD +55 -49
- pixeltable/exec/expr_eval_node.py +0 -232
- {pixeltable-0.3.0.dist-info → pixeltable-0.3.2.dist-info}/LICENSE +0 -0
- {pixeltable-0.3.0.dist-info → pixeltable-0.3.2.dist-info}/WHEEL +0 -0
- {pixeltable-0.3.0.dist-info → pixeltable-0.3.2.dist-info}/entry_points.txt +0 -0
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
import sys
|
|
3
|
-
import time
|
|
4
|
-
import warnings
|
|
5
|
-
from dataclasses import dataclass
|
|
6
|
-
from typing import Iterable, Optional
|
|
7
|
-
|
|
8
|
-
from tqdm import TqdmWarning, tqdm
|
|
9
|
-
|
|
10
|
-
from pixeltable import exprs
|
|
11
|
-
from pixeltable.func import CallableFunction
|
|
12
|
-
|
|
13
|
-
from .data_row_batch import DataRowBatch
|
|
14
|
-
from .exec_node import ExecNode
|
|
15
|
-
|
|
16
|
-
_logger = logging.getLogger('pixeltable')
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class ExprEvalNode(ExecNode):
|
|
20
|
-
"""Materializes expressions
|
|
21
|
-
"""
|
|
22
|
-
@dataclass
|
|
23
|
-
class Cohort:
|
|
24
|
-
"""List of exprs that form an evaluation context and contain calls to at most one external function"""
|
|
25
|
-
exprs_: list[exprs.Expr]
|
|
26
|
-
batched_fn: Optional[CallableFunction]
|
|
27
|
-
segment_ctxs: list['exprs.RowBuilder.EvalCtx']
|
|
28
|
-
target_slot_idxs: list[int]
|
|
29
|
-
batch_size: int = 8
|
|
30
|
-
|
|
31
|
-
def __init__(
|
|
32
|
-
self, row_builder: exprs.RowBuilder, output_exprs: Iterable[exprs.Expr], input_exprs: Iterable[exprs.Expr],
|
|
33
|
-
input: ExecNode
|
|
34
|
-
):
|
|
35
|
-
super().__init__(row_builder, output_exprs, input_exprs, input)
|
|
36
|
-
self.input_exprs = input_exprs
|
|
37
|
-
input_slot_idxs = {e.slot_idx for e in input_exprs}
|
|
38
|
-
# we're only materializing exprs that are not already in the input
|
|
39
|
-
self.target_exprs = [e for e in output_exprs if e.slot_idx not in input_slot_idxs]
|
|
40
|
-
self.pbar: Optional[tqdm] = None
|
|
41
|
-
self.cohorts: list[ExprEvalNode.Cohort] = []
|
|
42
|
-
self._create_cohorts()
|
|
43
|
-
|
|
44
|
-
def __next__(self) -> DataRowBatch:
|
|
45
|
-
input_batch = next(self.input)
|
|
46
|
-
# compute target exprs
|
|
47
|
-
for cohort in self.cohorts:
|
|
48
|
-
self._exec_cohort(cohort, input_batch)
|
|
49
|
-
_logger.debug(f'ExprEvalNode: returning {len(input_batch)} rows')
|
|
50
|
-
return input_batch
|
|
51
|
-
|
|
52
|
-
def _open(self) -> None:
|
|
53
|
-
warnings.simplefilter("ignore", category=TqdmWarning)
|
|
54
|
-
# This is a temporary hack. When B-tree indices on string columns were implemented (via computed columns
|
|
55
|
-
# that invoke the `BtreeIndex.str_filter` udf), it resulted in frivolous progress bars appearing on every
|
|
56
|
-
# insertion. This special-cases the `str_filter` call to suppress the corresponding progress bar.
|
|
57
|
-
# TODO(aaron-siegel) Remove this hack once we clean up progress bars more generally.
|
|
58
|
-
is_str_filter_node = all(
|
|
59
|
-
isinstance(expr, exprs.FunctionCall) and expr.fn.name == 'str_filter' for expr in self.output_exprs
|
|
60
|
-
)
|
|
61
|
-
if self.ctx.show_pbar and not is_str_filter_node:
|
|
62
|
-
self.pbar = tqdm(
|
|
63
|
-
total=len(self.target_exprs) * self.ctx.num_rows,
|
|
64
|
-
desc='Computing cells',
|
|
65
|
-
unit=' cells',
|
|
66
|
-
ncols=100,
|
|
67
|
-
file=sys.stdout
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
def _close(self) -> None:
|
|
71
|
-
if self.pbar is not None:
|
|
72
|
-
self.pbar.close()
|
|
73
|
-
|
|
74
|
-
def _get_batched_fn(self, expr: exprs.Expr) -> Optional[CallableFunction]:
|
|
75
|
-
if isinstance(expr, exprs.FunctionCall) and isinstance(expr.fn, CallableFunction) and expr.fn.is_batched:
|
|
76
|
-
return expr.fn
|
|
77
|
-
return None
|
|
78
|
-
|
|
79
|
-
def _is_batched_fn_call(self, expr: exprs.Expr) -> bool:
|
|
80
|
-
return self._get_batched_fn(expr) is not None
|
|
81
|
-
|
|
82
|
-
def _create_cohorts(self) -> None:
|
|
83
|
-
all_exprs = self.row_builder.get_dependencies(self.target_exprs)
|
|
84
|
-
# break up all_exprs into cohorts such that each cohort contains calls to at most one external function;
|
|
85
|
-
# seed the cohorts with only the ext fn calls
|
|
86
|
-
cohorts: list[list[exprs.Expr]] = []
|
|
87
|
-
current_batched_fn: Optional[CallableFunction] = None
|
|
88
|
-
for e in all_exprs:
|
|
89
|
-
if not self._is_batched_fn_call(e):
|
|
90
|
-
continue
|
|
91
|
-
assert isinstance(e, exprs.FunctionCall)
|
|
92
|
-
assert isinstance(e.fn, CallableFunction)
|
|
93
|
-
if current_batched_fn is None or current_batched_fn != e.fn:
|
|
94
|
-
# create a new cohort
|
|
95
|
-
cohorts.append([])
|
|
96
|
-
current_batched_fn = e.fn
|
|
97
|
-
cohorts[-1].append(e)
|
|
98
|
-
|
|
99
|
-
# expand the cohorts to include all exprs that are in the same evaluation context as the external calls;
|
|
100
|
-
# cohorts are evaluated in order, so we can exclude the target slots from preceding cohorts and input slots
|
|
101
|
-
exclude = set(e.slot_idx for e in self.input_exprs)
|
|
102
|
-
all_target_slot_idxs = set(e.slot_idx for e in self.target_exprs)
|
|
103
|
-
target_slot_idxs: list[list[int]] = [] # the ones materialized by each cohort
|
|
104
|
-
for i in range(len(cohorts)):
|
|
105
|
-
cohorts[i] = self.row_builder.get_dependencies(
|
|
106
|
-
cohorts[i], exclude=[self.row_builder.unique_exprs[slot_idx] for slot_idx in exclude])
|
|
107
|
-
target_slot_idxs.append(
|
|
108
|
-
[e.slot_idx for e in cohorts[i] if e.slot_idx in all_target_slot_idxs])
|
|
109
|
-
exclude.update(target_slot_idxs[-1])
|
|
110
|
-
|
|
111
|
-
all_cohort_slot_idxs = set(e.slot_idx for cohort in cohorts for e in cohort)
|
|
112
|
-
remaining_slot_idxs = set(all_target_slot_idxs) - all_cohort_slot_idxs
|
|
113
|
-
if len(remaining_slot_idxs) > 0:
|
|
114
|
-
cohorts.append(self.row_builder.get_dependencies(
|
|
115
|
-
[self.row_builder.unique_exprs[slot_idx] for slot_idx in remaining_slot_idxs],
|
|
116
|
-
exclude=[self.row_builder.unique_exprs[slot_idx] for slot_idx in exclude]))
|
|
117
|
-
target_slot_idxs.append(list(remaining_slot_idxs))
|
|
118
|
-
# we need to have captured all target slots at this point
|
|
119
|
-
assert all_target_slot_idxs == set().union(*target_slot_idxs)
|
|
120
|
-
|
|
121
|
-
for i in range(len(cohorts)):
|
|
122
|
-
cohort = cohorts[i]
|
|
123
|
-
# segment the cohort into sublists that contain either a single ext. function call or no ext. function calls
|
|
124
|
-
# (i.e., only computed cols)
|
|
125
|
-
assert len(cohort) > 0
|
|
126
|
-
# create the first segment here, so we can avoid checking for an empty list in the loop
|
|
127
|
-
segments = [[cohort[0]]]
|
|
128
|
-
is_batched_segment = self._is_batched_fn_call(cohort[0])
|
|
129
|
-
batched_fn: Optional[CallableFunction] = self._get_batched_fn(cohort[0])
|
|
130
|
-
for e in cohort[1:]:
|
|
131
|
-
if self._is_batched_fn_call(e):
|
|
132
|
-
segments.append([e])
|
|
133
|
-
is_batched_segment = True
|
|
134
|
-
batched_fn = self._get_batched_fn(e)
|
|
135
|
-
else:
|
|
136
|
-
if is_batched_segment:
|
|
137
|
-
# start a new segment
|
|
138
|
-
segments.append([])
|
|
139
|
-
is_batched_segment = False
|
|
140
|
-
segments[-1].append(e)
|
|
141
|
-
|
|
142
|
-
# we create the EvalCtxs manually because create_eval_ctx() would repeat the dependencies of each segment
|
|
143
|
-
segment_ctxs = [
|
|
144
|
-
exprs.RowBuilder.EvalCtx(
|
|
145
|
-
slot_idxs=[e.slot_idx for e in s], exprs=s, target_slot_idxs=[], target_exprs=[])
|
|
146
|
-
for s in segments
|
|
147
|
-
]
|
|
148
|
-
cohort_info = self.Cohort(cohort, batched_fn, segment_ctxs, target_slot_idxs[i])
|
|
149
|
-
self.cohorts.append(cohort_info)
|
|
150
|
-
|
|
151
|
-
def _exec_cohort(self, cohort: Cohort, rows: DataRowBatch) -> None:
|
|
152
|
-
"""Compute the cohort for the entire input batch by dividing it up into sub-batches"""
|
|
153
|
-
batch_start_idx = 0 # start row of the current sub-batch
|
|
154
|
-
# for multi-resolution models, we re-assess the correct ext fn batch size for each input batch
|
|
155
|
-
ext_batch_size = cohort.batched_fn.get_batch_size() if cohort.batched_fn is not None else None
|
|
156
|
-
if ext_batch_size is not None:
|
|
157
|
-
cohort.batch_size = ext_batch_size
|
|
158
|
-
|
|
159
|
-
while batch_start_idx < len(rows):
|
|
160
|
-
num_batch_rows = min(cohort.batch_size, len(rows) - batch_start_idx)
|
|
161
|
-
for segment_ctx in cohort.segment_ctxs:
|
|
162
|
-
if not self._is_batched_fn_call(segment_ctx.exprs[0]):
|
|
163
|
-
# compute batch row-wise
|
|
164
|
-
for row_idx in range(batch_start_idx, batch_start_idx + num_batch_rows):
|
|
165
|
-
self.row_builder.eval(
|
|
166
|
-
rows[row_idx], segment_ctx, self.ctx.profile, ignore_errors=self.ctx.ignore_errors)
|
|
167
|
-
else:
|
|
168
|
-
fn_call = segment_ctx.exprs[0]
|
|
169
|
-
assert isinstance(fn_call, exprs.FunctionCall)
|
|
170
|
-
# make a batched external function call
|
|
171
|
-
arg_batches: list[list[exprs.Expr]] = [[] for _ in range(len(fn_call.args))]
|
|
172
|
-
kwarg_batches: dict[str, list[exprs.Expr]] = {k: [] for k in fn_call.kwargs.keys()}
|
|
173
|
-
|
|
174
|
-
valid_batch_idxs: list[int] = [] # rows with exceptions are not valid
|
|
175
|
-
for row_idx in range(batch_start_idx, batch_start_idx + num_batch_rows):
|
|
176
|
-
row = rows[row_idx]
|
|
177
|
-
if row.has_exc(fn_call.slot_idx):
|
|
178
|
-
# one of our inputs had an exception, skip this row
|
|
179
|
-
continue
|
|
180
|
-
valid_batch_idxs.append(row_idx)
|
|
181
|
-
args, kwargs = fn_call._make_args(row)
|
|
182
|
-
for i in range(len(args)):
|
|
183
|
-
arg_batches[i].append(args[i])
|
|
184
|
-
for k in kwargs.keys():
|
|
185
|
-
kwarg_batches[k].append(kwargs[k])
|
|
186
|
-
num_valid_batch_rows = len(valid_batch_idxs)
|
|
187
|
-
|
|
188
|
-
if ext_batch_size is None:
|
|
189
|
-
# we need to choose a batch size based on the args
|
|
190
|
-
assert isinstance(fn_call.fn, CallableFunction)
|
|
191
|
-
sample_args = [arg_batches[i][0] for i in range(len(arg_batches))]
|
|
192
|
-
ext_batch_size = fn_call.fn.get_batch_size(*sample_args)
|
|
193
|
-
|
|
194
|
-
num_remaining_batch_rows = num_valid_batch_rows
|
|
195
|
-
while num_remaining_batch_rows > 0:
|
|
196
|
-
# we make ext. fn calls in batches of ext_batch_size
|
|
197
|
-
if ext_batch_size is None:
|
|
198
|
-
pass
|
|
199
|
-
num_ext_batch_rows = min(ext_batch_size, num_remaining_batch_rows)
|
|
200
|
-
ext_batch_offset = num_valid_batch_rows - num_remaining_batch_rows # offset into args, not rows
|
|
201
|
-
call_args = [
|
|
202
|
-
arg_batches[i][ext_batch_offset:ext_batch_offset + num_ext_batch_rows]
|
|
203
|
-
for i in range(len(arg_batches))
|
|
204
|
-
]
|
|
205
|
-
call_kwargs = {
|
|
206
|
-
k: kwarg_batches[k][ext_batch_offset:ext_batch_offset + num_ext_batch_rows]
|
|
207
|
-
for k in kwarg_batches.keys()
|
|
208
|
-
}
|
|
209
|
-
start_ts = time.perf_counter()
|
|
210
|
-
assert isinstance(fn_call.fn, CallableFunction)
|
|
211
|
-
result_batch = fn_call.fn.exec_batch(call_args, call_kwargs)
|
|
212
|
-
self.ctx.profile.eval_time[fn_call.slot_idx] += time.perf_counter() - start_ts
|
|
213
|
-
self.ctx.profile.eval_count[fn_call.slot_idx] += num_ext_batch_rows
|
|
214
|
-
|
|
215
|
-
# move the result into the row batch
|
|
216
|
-
for result_idx in range(len(result_batch)):
|
|
217
|
-
row_idx = valid_batch_idxs[ext_batch_offset + result_idx]
|
|
218
|
-
row = rows[row_idx]
|
|
219
|
-
row[fn_call.slot_idx] = result_batch[result_idx]
|
|
220
|
-
|
|
221
|
-
num_remaining_batch_rows -= num_ext_batch_rows
|
|
222
|
-
|
|
223
|
-
# switch to the ext fn batch size
|
|
224
|
-
cohort.batch_size = ext_batch_size
|
|
225
|
-
|
|
226
|
-
# make sure images for stored cols have been saved to files before moving on to the next batch
|
|
227
|
-
rows.flush_imgs(
|
|
228
|
-
slice(batch_start_idx, batch_start_idx + num_batch_rows), self.stored_img_cols, self.flushed_img_slots)
|
|
229
|
-
if self.pbar is not None:
|
|
230
|
-
self.pbar.update(num_batch_rows * len(cohort.target_slot_idxs))
|
|
231
|
-
batch_start_idx += num_batch_rows
|
|
232
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|