MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / has_function

Method has_function

op_builder/builder.py:334–420  ·  view source on GitHub ↗

Test for existence of a function within a tuple of libraries. This is used as a smoke test to check whether a certain library is available. As a test, this creates a simple C program that calls the specified function, and then distutils is used to compile that progr

(self, funcname, libraries, library_dirs=None, verbose=False)

Source from the content-addressed store, hash-verified

332 return []
333
334 def has_function(self, funcname, libraries, library_dirs=None, verbose=False):
335 '''
336 Test for existence of a function within a tuple of libraries.
337
338 This is used as a smoke test to check whether a certain library is available.
339 As a test, this creates a simple C program that calls the specified function,
340 and then distutils is used to compile that program and link it with the specified libraries.
341 Returns True if both the compile and link are successful, False otherwise.
342 '''
343 # Imported here because distutils pulls in setuptools, which slows down import deepspeed.
344 import distutils.ccompiler
345 import distutils.log
346 import distutils.sysconfig
347 from distutils.errors import CompileError, LinkError
348
349 tempdir = None # we create a temporary directory to hold various files
350 filestderr = None # handle to open file to which we redirect stderr
351 oldstderr = None # file descriptor for stderr
352 try:
353 # Echo compile and link commands that are used.
354 if verbose:
355 distutils.log.set_verbosity(1)
356
357 # Create a compiler object.
358 compiler = distutils.ccompiler.new_compiler(verbose=verbose)
359
360 # Configure compiler and linker to build according to Python install.
361 distutils.sysconfig.customize_compiler(compiler)
362
363 # Create a temporary directory to hold test files.
364 tempdir = tempfile.mkdtemp()
365
366 # Define a simple C program that calls the function in question
367 prog = "void %s(void); int main(int argc, char** argv) { %s(); return 0; }" % (funcname, funcname)
368
369 # Write the test program to a file.
370 filename = os.path.join(tempdir, 'test.c')
371 with open(filename, 'w') as f:
372 f.write(prog)
373
374 # Redirect stderr file descriptor to a file to silence compile/link warnings.
375 if not verbose:
376 filestderr = open(os.path.join(tempdir, 'stderr.txt'), 'w')
377 oldstderr = os.dup(sys.stderr.fileno())
378 os.dup2(filestderr.fileno(), sys.stderr.fileno())
379
380 # Workaround for behavior in distutils.ccompiler.CCompiler.object_filenames()
381 # Otherwise, a local directory will be used instead of tempdir
382 drive, driveless_filename = os.path.splitdrive(filename)
383 root_dir = driveless_filename[0] if os.path.isabs(driveless_filename) else ''
384 output_dir = os.path.join(drive, root_dir)
385
386 # Attempt to compile the C program into an object file.
387 cflags = shlex.split(os.environ.get('CFLAGS', ""))
388 objs = compiler.compile([filename], output_dir=output_dir, extra_preargs=self.strip_empty_entries(cflags))
389
390 # Attempt to link the object file into an executable.
391 # Be sure to tack on any libraries that have been specified.

Callers 7

is_compatibleMethod · 0.80
is_compatibleMethod · 0.80
is_compatibleMethod · 0.80
is_compatibleMethod · 0.80
is_compatibleMethod · 0.80
is_compatibleMethod · 0.80
is_compatibleMethod · 0.80

Calls 6

strip_empty_entriesMethod · 0.95
writeMethod · 0.45
filenoMethod · 0.45
getMethod · 0.45
compileMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected