Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt711 committed Jun 5, 2024
1 parent ec0853c commit 2aa159c
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions docs/cudf/source/developer_guide/cudf_pandas.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# cudf.pandas
The use of the cuDF pandas accelerator mode (`cudf.pandas`) is explained [here](../cudf_pandas/index.rst).
The purpose of this document is to explain how the fast-slow proxy mechanism works and document the internal environment variables used to debug `cudf.pandas`.
The use of the cuDF pandas accelerator mode (`cudf.pandas`) is explained [in the user guide](../cudf_pandas/index.rst).
The purpose of this document is to explain how the fast-slow proxy mechanism works and document internal environment variables that can be used to debug `cudf.pandas` itself.

## fast-slow proxy mechanism:
`cudf.pandas` works by wrapping each Pandas type and its corresponding cuDF type in new proxy types (aka fast-slow proxy types).
The purpose of the proxy types is to attempt computations on the fast (cuDF) object first, and then fall back to running on the slow (Pandas) object if the fast version fails.
## fast-slow proxy mechanism
`cudf.pandas` works by wrapping each Pandas type and its corresponding cuDF type in a new proxy type also known as a fast-slow proxy type.
The purpose of proxy types is to attempt computations on the fast (cuDF) object first, and then fall back to running on the slow (Pandas) object if the fast version fails.

### Types:
#### Wrapped Types and Proxy Types:
#### Wrapped Types and Proxy Types
The "wrapped" types/classes are the Pandas and cuDF specific types that have been wrapped into proxy types.
Wrapped objects and proxy objects are instances of wrapped types and proxy types, respectively.
In the snippet below `s1` and `s2` are wrapped objects and `s3` is a fast-slow proxy object.
Expand All @@ -25,25 +25,28 @@ Also note that the module `xpd` is a wrapped module and contains cuDF and Pandas
s3 = xpd.Series([1,2])
```

```{note}
Note that users should never have to interact with the wrapped objects directly in this way.
This code is purely for demonstrative purposes.
```

#### The Different Kinds of Proxy Types:
#### The Different Kinds of Proxy Types
In `cudf.pandas`, there are two main kinds of proxy types: final types and intermediate types.

##### Final and Intermediate Proxy Types:
##### Final and Intermediate Proxy Types
Final types are types for which known operations exist for converting an object of a "fast" type to a "slow" type and vice versa.
For example, `cudf.DataFrame` can be converted to Pandas using the method `to_pandas`, and `pd.DataFrame` can be converted to cuDF using the function `cudf.from_pandas`.
Intermediate types are the types of the results of operations invoked on final types.
For example, `xpd.DataFrameGroupBy` is an intermediate type that will be created during a groupby operation on the final type `xpd.DataFrame`.

##### Attributes and Callable Proxy Types:
##### Attributes and Callable Proxy Types
Final proxy types are typically classes or modules, both of which have attributes.
Classes also have methods.
These attributes and methods must be wrapped as well to support the fast-slow proxy scheme.

#### Creating New Proxy Types:
`_FinalProxy` and `_IntermediateProxy` types are created using the functions `make_final_proxy_type` and `make_intermediate_proxy` type, respectively. Creating a new final type looks like this.
#### Creating New Proxy Types
`_FinalProxy` and `_IntermediateProxy` types are created using the functions `make_final_proxy_type` and `make_intermediate_proxy` type, respectively.
Creating a new final type looks like this.

```python
DataFrame = make_final_proxy_type(
Expand All @@ -56,8 +59,7 @@ DataFrame = make_final_proxy_type(
```

### The Fallback Mechanism
The `_fast_slow_function_call` is the all-important function that we use to call operations the fast way (using cuDF) and if that fails, the slow way (using Pandas).
The is also known as the fallback mechanism.
Proxied calls are implemented with fallback via [`_fast_slow_function_call`](https://github.com/rapidsai/cudf/blob/57aeeb78d85e169ac18b82f51d2b1cbd01b0608d/python/cudf/cudf/pandas/fast_slow_proxy.py#L869). This implements the mechanism by which we attempt operations the fast way (using cuDF) and then fall back to the slow way (using Pandas) on failure.
The function looks like this:
```python
def _fast_slow_function_call(func: Callable, *args, **kwargs):
Expand All @@ -73,19 +75,19 @@ def _fast_slow_function_call(func: Callable, *args, **kwargs):
...
return _maybe_wrap_result(result, func, *args, **kwargs), fast
```
As you can see the function attempts to call `func` the fast way using cuDF and if any `Exception` occurs, it calls the function using Pandas.
In essence, this `try-except` is what allows `cudf.pandas` to support 100% of the Pandas API.
As we can see the function attempts to call `func` the fast way using cuDF and if any `Exception` occurs, it calls the function using Pandas.
In essence, this `try-except` is what allows `cudf.pandas` to support the bulk of the Pandas API.

At the end, the function wraps the result from either path in a fast-slow proxy object, if necessary.

#### Converting Proxy Objects
Note that before the `func` is called, the proxy object and its attributes need to be converted to either their cuDF or Pandas implementations.
This conversion is handled in the function `_transform_arg` which both `_fast_arg` and `_slow_arg` call.

`_transform_arg` is a recuirsive function that will call itself depending on the type or argument passed to it (eg. `_transform_arg` is called for each element in a list of arguments).
`_transform_arg` is a recursive function that will call itself depending on the type or argument passed to it (eg. `_transform_arg` is called for each element in a list of arguments).

### Using Metaclasses
`cudf.pandas` uses a [metaclass](https://docs.python.org/3/glossary.html#term-metaclass) called (`_FastSlowProxyMeta`) to dynamically find class attributes and classmethods of fast-slow proxy types.
`cudf.pandas` uses a [metaclass](https://docs.python.org/3/glossary.html#term-metaclass) called (`_FastSlowProxyMeta`) to find class attributes and classmethods of fast-slow proxy types.
For example, in the snippet below, the `xpd.Series` type is an instance of `_FastSlowProxyMeta`.
Therefore we can access the property `_fsproxy_fast` defined in the metaclass.
```python
Expand Down

0 comments on commit 2aa159c

Please sign in to comment.