Skip to content

Commit

Permalink
Vertex AI Vision - Viper Preview Launch.
Browse files Browse the repository at this point in the history
Supporting SDK.

PiperOrigin-RevId: 534212720
  • Loading branch information
dchao34 committed May 24, 2023
1 parent 1c2696f commit 6fc1c13
Show file tree
Hide file tree
Showing 265 changed files with 24,025 additions and 1,078 deletions.
4 changes: 4 additions & 0 deletions BUILD
@@ -1,3 +1,7 @@
# Copyright (c) 2023 Google LLC All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix
Expand Down
3 changes: 2 additions & 1 deletion WORKSPACE
Expand Up @@ -47,6 +47,7 @@ switched_rules_by_language(
cc = True,
go = True,
grpc = True,
python = True,
)

# Requirements for @com_github_grpc_grpc.
Expand All @@ -72,4 +73,4 @@ rules_proto_dependencies()

rules_proto_toolchains()

# comment
# comments
3 changes: 3 additions & 0 deletions bazel/BUILD
@@ -0,0 +1,3 @@
# Copyright (c) 2023 Google LLC All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
12 changes: 6 additions & 6 deletions bazel/BUILD.opencv4
Expand Up @@ -17,13 +17,13 @@ cmake_external(
"make -j7",
"make install",
],
out_include_dir = "include/opencv4",
out_include_dir = "include/",
shared_libraries = [
"libopencv_core.so.406",
"libopencv_highgui.so.406",
"libopencv_imgcodecs.so.406",
"libopencv_imgproc.so.406",
"libopencv_videoio.so.406",
"libopencv_core.so.407",
"libopencv_highgui.so.407",
"libopencv_imgcodecs.so.407",
"libopencv_imgproc.so.407",
"libopencv_videoio.so.407",
],
visibility = ["//visibility:public"],
)
22 changes: 12 additions & 10 deletions bazel/opencv.BUILD
Expand Up @@ -7,15 +7,17 @@
# Follow the instructions to install opencv there (pick ubuntu:focal for opencv 3.4).
cc_library(
name = "opencv",
linkopts = [
"-l:libopencv_core.so",
"-l:libopencv_calib3d.so",
"-l:libopencv_features2d.so",
"-l:libopencv_highgui.so",
"-l:libopencv_imgcodecs.so",
"-l:libopencv_imgproc.so",
"-l:libopencv_video.so",
"-l:libopencv_videoio.so",
],
srcs = glob(
[
"lib/libopencv_core.so",
"lib/libopencv_highgui.so",
"lib/libopencv_imgcodecs.so",
"lib/libopencv_imgproc.so",
"lib/libopencv_video.so",
"lib/libopencv_videoio.so",
],
),
hdrs = glob(["include/opencv4/opencv2/**/*.h*"]),
includes = ["include"],
visibility = ["//visibility:public"],
)
34 changes: 15 additions & 19 deletions bazel/visionai_deps.bzl
Expand Up @@ -60,26 +60,12 @@ def visionai_deps(is_gob_source_only = False):
sha256 = "b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5",
)

protobuf_version = "3.20.3"
protobuf_sha256 = "acb71ce46502683c31d4f15bafb611b9e7b858b6024804d6fb84b85750884208"
protobuf_urls = [
"https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protobuf-all-3.20.3.tar.gz",
]

# NOTE: The OSS version works okay for this one as well.
if is_gob_source_only:
protobuf_version = "3.19.1"
protobuf_sha256 = "87407cd28e7a9c95d9f61a098a53cf031109d451a7763e7dd1253abf8b4df422"
protobuf_urls = [
"https://github.com/protocolbuffers/protobuf/archive/v{}.tar.gz".format(protobuf_version),
]

maybe(
http_archive,
name = "com_google_protobuf",
sha256 = protobuf_sha256,
strip_prefix = "protobuf-{}".format(protobuf_version),
urls = protobuf_urls,
sha256 = "acb71ce46502683c31d4f15bafb611b9e7b858b6024804d6fb84b85750884208",
strip_prefix = "protobuf-{}".format("3.20.3"),
urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protobuf-all-3.20.3.tar.gz"],
)

maybe(
Expand All @@ -90,6 +76,16 @@ def visionai_deps(is_gob_source_only = False):
sha256 = "dcf71b9cba8dc0ca9940c4b316a0c796be8fab42b070bb6b7cab62b48f0e66c4",
)

maybe(
http_archive,
name = "absl_py",
sha256 = "0fb3a4916a157eb48124ef309231cecdfdd96ff54adf1660b39c0d4a9790a2c0",
strip_prefix = "abseil-py-1.4.0",
urls = [
"https://github.com/abseil/abseil-py/archive/refs/tags/v1.4.0.tar.gz",
],
)

maybe(
http_archive,
name = "com_github_tencent_rapidjson",
Expand Down Expand Up @@ -229,7 +225,7 @@ def visionai_deps(is_gob_source_only = False):

maybe(
http_archive,
name = "com_github_google_re2",
name = "com_googlesource_code_re2",
sha256 = "9f3b65f2e0c78253fcfdfce1754172b0f97ffdb643ee5fd67f0185acf91a3f28",
strip_prefix = "re2-2022-06-01",
url = "https://github.com/google/re2/archive/refs/tags/2022-06-01.zip",
Expand All @@ -240,7 +236,7 @@ def visionai_deps(is_gob_source_only = False):
native.new_local_repository,
name = "linux_opencv",
build_file = "//bazel:opencv.BUILD",
path = "/usr",
path = "/usr/local/opencv",
)

maybe(
Expand Down
260 changes: 260 additions & 0 deletions ddcterm
@@ -0,0 +1,260 @@
#! /usr/bin/env python3
"""A tool that manages the terminal access of a development docker container.
This tool manages a build/test environment in a docker container during
code development. It is assumed that each directory is associated with one
development container.
The workflow is as follows:
To start the development container:
$ ./ddcterm up <docker_image_spec>
where <docker_image_spec> is the full docker image specification.
To enter the development container:
$ ./ddcterm enter
To end the development container:
$ ./ddcterm down
"""

import argparse
import getpass
import hashlib
import logging
import os
import os.path
import pwd
import shlex
import subprocess as sp
import sys

_CONTAINER_WORKDIR = "/workdir"
_CONTAINER_SHELL = "/bin/bash"

logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)


def _get_user_info_dict():
user_info_struct = pwd.getpwnam(getpass.getuser())
return {
"username": user_info_struct.pw_name,
"uid": user_info_struct.pw_uid,
"gid": user_info_struct.pw_gid
}


def _container_name_from_cwd():
return "{}-{}".format(
os.path.basename(__file__),
hashlib.sha256(os.getcwd().encode("latin-1")).hexdigest())


def _is_container_running(container_name):
docker_inspect_command = ("docker inspect "
"-f '{{{{.State.Running}}}}' "
"{container_name}").format(
container_name=container_name)
try:
sp.check_output(shlex.split(docker_inspect_command), stderr=sp.STDOUT)
except sp.CalledProcessError:
return False
return True


def _is_image_local(image_name):
docker_inspect_command = ("docker inspect {image_name}").format(
image_name=image_name)
try:
sp.check_output(shlex.split(docker_inspect_command), stderr=sp.STDOUT)
except sp.CalledProcessError:
return False
return True


def up(args):
"""Bring up the development container."""

container_name = _container_name_from_cwd()

if _is_container_running(container_name):
logging.warning("A container is already up: %s", container_name)
sys.exit(1)

if not _is_image_local(args.image_spec):
logging.error(
"%s is not found locally.\nPlease run `docker pull %s` and try again.",
args.image_spec, args.image_spec)
sys.exit(1)

def _docker_display_flags():
return "-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY -e XAUTHORITY"

def _docker_gdb_enable_ptrace_flags():
return "--cap-add=SYS_PTRACE --security-opt seccomp=unconfined"

# Run the docker container based on the given image spec.
docker_run_config = {
"gpus":
"" if args.no_nvidia else "--gpus all",
"gdb_ptrace":
"" if args.no_gdb_ptrace else _docker_gdb_enable_ptrace_flags(),
"host_network":
"" if args.no_host_network else "--network host",
"host_display":
"" if args.no_host_display else _docker_display_flags(),
"host_sourcedir":
os.getcwd(),
"container_workdir":
_CONTAINER_WORKDIR,
"container_name":
container_name,
"image_spec":
args.image_spec,
"container_shell":
_CONTAINER_SHELL,
}
docker_run_command = ("docker run "
"-t -d --rm "
"{gpus} "
"{gdb_ptrace} "
"{host_network} "
"{host_display} "
"-v {host_sourcedir}:{container_workdir} "
"-w {container_workdir} "
"--name {container_name} "
"{image_spec} {container_shell}").format(
**docker_run_config)
try:
sp.check_output(shlex.split(docker_run_command))
except sp.CalledProcessError as e:
logging.error("%s", e.output.decode("UTF-8"))
sys.exit(1)

# Create a non-root user in the container that agrees with the host user.
user_info = _get_user_info_dict()
docker_exec_user_config = {
"username": user_info["username"],
"groupname": user_info["username"],
"uid": user_info["uid"],
"gid": user_info["gid"],
"container_name": docker_run_config["container_name"],
}
docker_exec_command = ("docker exec "
"{container_name} "
"groupadd -g {gid} "
"{groupname}").format(**docker_exec_user_config)
sp.check_output(shlex.split(docker_exec_command))

docker_exec_command = ("docker exec "
"{container_name} "
"useradd -g {gid} -u {uid} -m "
"{username}").format(**docker_exec_user_config)
sp.check_output(shlex.split(docker_exec_command))

logging.info("%s is up.", container_name)


def enter(args):
"""Enter the development container."""

container_name = _container_name_from_cwd()
if not _is_container_running(container_name):
logging.error("Container %s is not running.", container_name)
sys.exit(1)

user_info = _get_user_info_dict()
docker_exec_config = {
"container_name":
_container_name_from_cwd(),
"user_flag":
"" if args.root else "-u {uid}:{gid}".format(**user_info),
"alsologtostderr":
"" if args.no_alsologtostderr else "-e GLOG_alsologtostderr=1",
"gst_plugin_path":
"-e GST_PLUGIN_PATH={}".format(args.gst_plugin_path),
"container_shell":
_CONTAINER_SHELL,
}

logging.info("Entering container as \'%s\'",
"root" if args.root else user_info["username"])

docker_exec_command = ("docker exec "
"-it "
"{user_flag} "
"{alsologtostderr} "
"{gst_plugin_path} "
"{container_name} "
"{container_shell}").format(**docker_exec_config)
docker_exec_tokens = shlex.split(docker_exec_command)
os.execlp(docker_exec_tokens[0], *docker_exec_tokens)


def down(args): # pylint: disable=unused-argument
"""Bring down the development container."""

container_name = _container_name_from_cwd()
if not _is_container_running(container_name):
logging.error("Container %s is not running.", container_name)
sys.exit(1)

docker_stop_command = "docker container stop {}".format(container_name)
sp.check_output(shlex.split(docker_stop_command))
logging.info("%s is down.", container_name)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Docker development container terminal.")
subparsers = parser.add_subparsers()

# Options for "up" .
subparser_up = subparsers.add_parser("up")
subparser_up.add_argument(
"image_spec",
type=str,
help="The docker image spec in which the terminal will be run.")
subparser_up.add_argument(
"--no-gdb-ptrace",
action="store_true",
help="Do not enable ptrace for gdb.")
subparser_up.add_argument(
"--no-host-display",
action="store_true",
help="Do not connect to the host display.")
subparser_up.add_argument(
"--no-host-network",
action="store_true",
help="Run the container without using the host network interfaces.")
subparser_up.add_argument(
"--no-nvidia",
action="store_true",
help="Run the container without enabling GPUs.")
subparser_up.set_defaults(func=up)

# Options for "enter".
subparser_enter = subparsers.add_parser("enter")
subparser_enter.add_argument(
"--root", action="store_true", help="Enter the container as root.")
subparser_enter.add_argument(
"--no-alsologtostderr",
action="store_true",
help="Do not set GLOG_alsologtostderr")
subparser_enter.add_argument(
"--gst-plugin-path",
type=str,
default="/workdir/visionai-sdk/bazel-bin/visionai/gstreamer/plugins:${GST_PLUGIN_PATH}",
help="Value for GST_PLUGIN_PATH")
subparser_enter.set_defaults(func=enter)

# Options for "down".
subparser_down = subparsers.add_parser("down")
subparser_down.set_defaults(func=down)

if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)

parsed_args = parser.parse_args()
parsed_args.func(parsed_args)

0 comments on commit 6fc1c13

Please sign in to comment.