Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: [LS-2561] Stream compress multipart runs #1316

Merged
merged 57 commits into from
Dec 21, 2024

Conversation

angus-langchain
Copy link
Contributor

@angus-langchain angus-langchain commented Dec 10, 2024

Purpose

Compresses multipart runs into a buffer and streams the compressed data to the API. Compatible with https://github.com/langchain-ai/langchainplus/pull/7317

@angus-langchain angus-langchain changed the title Angus/stream compress multipart perf: Stream compress multipart runs Dec 10, 2024
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_operations.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_operations.py Show resolved Hide resolved
python/langsmith/_internal/_operations.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_background_thread.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_background_thread.py Outdated Show resolved Hide resolved
if self.compress_traces:
self.boundary = BOUNDARY
self.compressed_runs_buffer: io.BytesIO = io.BytesIO()
self.compressor_writer: zstd.ZstdCompressionWriter = zstd.ZstdCompressor(level=3).stream_writer(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use the lowest level, we want to minimize cpu usage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With level=1 we're only getting a compression ratio of 6.7 vs 18+, level=2 puts us around 12. I'm open to experimenting with this to see if we still OOM with level=1

@angus-langchain angus-langchain marked this pull request as ready for review December 11, 2024 01:10
@angus-langchain angus-langchain changed the title perf: Stream compress multipart runs perf: [LS-2561] Stream compress multipart runs Dec 13, 2024
Copy link
Collaborator

@hinthornw hinthornw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to copy the zstandard's BSD license somewhere:

Copyright (c) 2016, Gregory Szorc
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@@ -37,6 +37,7 @@ requests-toolbelt = "^1.0.0"

# Enabled via `langsmith_pyo3` extra: `pip install langsmith[langsmith_pyo3]`.
langsmith-pyo3 = { version = "^0.1.0rc2", optional = true }
zstandard = "^0.23.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like they have a conda project so should be good, but we may have to manually update our conda distro for the SDK
https://anaconda.org/conda-forge/langsmith
https://anaconda.org/conda-forge/zstandard

Copy link
Contributor Author

@angus-langchain angus-langchain Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. Would this issue come up when creating a new version of the sdk?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya upon release. It should be straightforward to support just noting!

python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/client.py Show resolved Hide resolved
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_operations.py Outdated Show resolved Hide resolved
)

if (size_limit_bytes is None or current_size < size_limit_bytes) and (
size_limit is None or client._run_count < size_limit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 100?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the limit that is currently used. It also performed well in flush time benchmarks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can also be overridden by the batch ingest config

python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/client.py Outdated Show resolved Hide resolved
python/langsmith/_internal/_background_thread.py Outdated Show resolved Hide resolved
@angus-langchain angus-langchain force-pushed the angus/stream-compress-multipart branch from 856e4a4 to 2ac7a35 Compare December 20, 2024 16:50
_SIZE_LIMIT_BYTES = 20_971_520 # 20MB by default
_AUTO_SCALE_UP_QSIZE_TRIGGER = 200
_AUTO_SCALE_UP_NTHREADS_LIMIT = 32
_AUTO_SCALE_DOWN_NEMPTY_TRIGGER = 4
_BLOCKSIZE_BYTES = 1024 * 1024 # 1MB
_BOUNDARY = uuid.uuid4().hex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be unique every time? or should we hardcode it here?

@angus-langchain angus-langchain merged commit 0243f79 into main Dec 21, 2024
9 checks passed
@angus-langchain angus-langchain deleted the angus/stream-compress-multipart branch December 21, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants